//为以下拼接请求地址
public String path="";
public String key="";
public String pno="";
public String ps="";
try {
// 创建客户端
HttpClient client = new DefaultHttpClient();
// 创建post对象
HttpPost post = new HttpPost(path);
ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("key", key));
list.add(new BasicNameValuePair("pno", pno));
list.add(new BasicNameValuePair("ps", ps));
HttpEntity entity = new UrlEncodedFormEntity(list);
// 发送内容
post.setEntity(entity);
// 发送请求
HttpResponse response = client.execute(post);
//得到请求码
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200)
{
// 得到实体内容
InputStream inputStream = response.getEntity()
.getContent();
int len;
byte[] b = new byte[1024];
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
while ((len = inputStream.read(b)) != -1)
{
arrayOutputStream.write(b, 0, len);
}
String strJson = arrayOutputStream.toString();
//handler向主线程发送信息,更新数据
handler.obtainMessage(SUCCESS, strJson).sendToTarget();
}
} catch (Exception e) {
e.printStackTrace();
}
HttpClient——Post请求
最新推荐文章于 2022-06-14 13:25:16 发布