android客户端通过get,post进行数据交互

来源:互联网 发布:数据分类汇总步骤 编辑:程序博客网 时间:2024/04/30 13:09

【get方式】

protected String getRequest(String url, DefaultHttpClient client) throws Exception {

DefaultHttpClient client = new DefaultHttpClient(new BasicHttpParams());

//超时请求

        client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);

        //读取超时

       client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);

 

String result = null;

int statusCode = 0;

HttpGet getMethod = new HttpGet(url);

try { 

HttpResponse httpResponse = client.execute(getMethod);

//statusCode == 200 正常

statusCode = httpResponse.getStatusLine().getStatusCode();

//处理返回的httpResponse信息

result = retrieveInputStream(httpResponse.getEntity());

} catch (Exception e) {

} finally {

getMethod.abort();

}

return result;

}

/**

* 处理httpResponse信息,返回String

* @param httpEntity

* @return String

*/

protected String retrieveInputStream(HttpEntity httpEntity) {

Long l = httpEntity.getContentLength();

int length = (int) httpEntity.getContentLength();

//the number of bytes of the content, or a negative number if unknown. If the content length is known but exceeds Long.MAX_VALUE, a 

 

negative number is returned.

//length==-1,下面这句报错,println needs a message

//System.out.println("length = "+length);

if (length < 0) length = 10000;

StringBuffer stringBuffer = new StringBuffer(length);

try {

InputStreamReader inputStreamReader = new InputStreamReader(httpEntity.getContent(), HTTP.UTF_8);

char buffer[] = new char[length];

int count;

while ((count = inputStreamReader.read(buffer, 0, length - 1)) > 0) {

stringBuffer.append(buffer, 0, count);

}

}catch(IOException e){

e.printStackTrace();

}

return stringBuffer.toString();

}

 

 

【post方式】

 

//////

List<NameValuePair> nameValuePairs = new List<NameValuePair>();//构建post给php的参数

nameValuePairs.add(new BasicNameValuePair("key","value"));

 

    //通过post获得数据

    public static String postHttpData(String url,List<NameValuePair> nameValuePairs)

    {

        String resultStr=null;

        HttpClient httpclient = new DefaultHttpClient();

//超时请求

        httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);

        //读取超时

        httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);

 

        HttpPost httppost = new HttpPost(url);

         try {

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response; 

                response=httpclient.execute(httppost);     

                resultStr=retrieveInputStream(httpResponse.getEntity());

            } catch (UnsupportedEncodingException e) {

                Log.d(url, "UnsupportedEncodingException");

                e.printStackTrace();

            } catch (ClientProtocolException e) {

                Log.d(url, "ClientProtocolException");

                e.printStackTrace();

            } catch (IOException e) {

                Log.d(url, "IOException");

                e.printStackTrace();

            }

            return resultStr; 

    }

 

get的参数设置是有长度限制的,在用json传递大数据量时,get方式很可能超过长度。

post的参数没有长度限制,可以放心使用。

注意超时的部分,网络不稳定或是其他情况,可是非常有必要的,

不然android客户端卡住就影响用户体验了。