android get请求参数带/,ANDROID_MARS学习笔记_S04_004_用HTTPCLENT发带参数的get和post请求...

1 packagecom.http2;2

3 importjava.io.BufferedReader;4 importjava.io.IOException;5 importjava.io.InputStream;6 importjava.io.InputStreamReader;7 importjava.io.UnsupportedEncodingException;8 importjava.util.ArrayList;9 importjava.util.List;10

11 importorg.apache.http.HttpEntity;12 importorg.apache.http.HttpResponse;13 importorg.apache.http.NameValuePair;14 importorg.apache.http.client.ClientProtocolException;15 importorg.apache.http.client.HttpClient;16 importorg.apache.http.client.entity.UrlEncodedFormEntity;17 importorg.apache.http.client.methods.HttpGet;18 importorg.apache.http.client.methods.HttpPost;19 importorg.apache.http.client.methods.HttpRequestBase;20 importorg.apache.http.impl.client.DefaultHttpClient;21 importorg.apache.http.message.BasicNameValuePair;22

23 importandroid.os.AsyncTask;24

25 public class HttpAsyncTask extends AsyncTask{26

27 privateString url;28 privateString method;29 private Listparams;30

31 publicHttpAsyncTask(String url, String method) {32 super();33 this.url =url;34 this.method =method;35 }36 public HttpAsyncTask(String url, String method, Listparams) {37 super();38 this.url =url;39 this.method =method;40 this.params =params;41 }42

43 @Override44 protectedVoid doInBackground(String... params) {45 if("get".equals(method))46 sendGetHttpRequest(url);47 else if("post".equals(method))48 sendPostRequest(url, this.params);49 return null;50 }51

52 private void sendPostRequest(String url,Listparams) {53 System.out.println("sendPostRequest---->");54 NameValuePair nameValuePair1 = new BasicNameValuePair("name", this.params.get(0));55 NameValuePair nameValuePair2 = new BasicNameValuePair("age", this.params.get(1));56 List nameValuePairs = new ArrayList();57 nameValuePairs.add(nameValuePair1);58 nameValuePairs.add(nameValuePair2);59 InputStream inputStream = null;60 try{61 //请求体

62 HttpEntity httpEntity = newUrlEncodedFormEntity(nameValuePairs);63 //url不用带参数-->"?a=1&b=2"

64 HttpPost httpPost = newHttpPost(url);65 httpPost.setEntity(httpEntity);66 HttpClient httpClient = newDefaultHttpClient();67 HttpResponse httpResponse =httpClient.execute(httpPost);68 HttpEntity respHttpEntity =httpResponse.getEntity();69 inputStream =respHttpEntity.getContent();70 BufferedReader reader = new BufferedReader(newInputStreamReader(inputStream));71 String line = null;72 StringBuilder sb = newStringBuilder();73 while ((line = reader.readLine()) != null) {74 sb.append(line);75 }76 System.out.println(sb);77 } catch(Exception e) {78 e.printStackTrace();79 }80 }81

82 private voidsendGetHttpRequest(String url) {83 System.out.println("sendGetHttpRequest---->");84 //生成一个请求对象

85 HttpGet httpGet = newHttpGet(url);86 //生成一个Http客户端对象

87 HttpClient httpClient = newDefaultHttpClient();88 InputStream inputStream = null;89 //使用Http客户端发送请求对象

90 try{91 HttpResponse httpResponse =httpClient.execute(httpGet);92 HttpEntity httpEntity =httpResponse.getEntity();93 inputStream =httpEntity.getContent();94 BufferedReader reader = new BufferedReader(newInputStreamReader(inputStream));95 String line = null;96 StringBuilder sb = newStringBuilder();97 while ((line = reader.readLine()) != null) {98 sb.append(line);99 }100 System.out.println(sb);101 } catch(ClientProtocolException e) {102 e.printStackTrace();103 } catch(IOException e) {104 e.printStackTrace();105 } finally{106 try{107 inputStream.close();108 } catch(IOException e) {109 e.printStackTrace();110 }111 }112 }113

114 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值