/**
* 以Post的方式向服务器提交数据,只返回响应的代码
*
* @param url
* 请求的地址
* @param paramList
* 参数列表
* @return
*/
public static String post(String url, HashMap<String, String> paramList) {
if (url == null) {
throw new NullPointerException();
}
String html = null;
int resultCode = -1;
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>(); int dataSize = paramList.size();
Object[] keys = paramList.keySet().toArray();
for (int i = 0; i < dataSize; i++) {
String key = keys[i].toString();
params.add(new BasicNameValuePair(key, paramList.get(key)));
}
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse get_response = httpClient.execute(request); // 执行请求
HttpPost postMethod = new HttpPost(url);
Header[] headers = get_response.getAllHeaders();// 获取所有的头信息
for (Header header : headers) {
postMethod.addHeader(header.getName(),header.getValue());
}
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); // 将参数填入POST
// Entity中
HttpResponse post_response = httpClient.execute(postMethod); // 执行POST方法
resultCode = post_response.getStatusLine().getStatusCode();// 获取响应代码
System.out.println("相应代码" + resultCode);
if (resultCode == 200) {
html = EntityUtils.toString(post_response.getEntity(), "utf-8");// 获取响应内容
System.out.println(html);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return resultCode;
return html;
}大家看看上面的代码,为什么我在执行post之前设置了HTTP头信息之后就没法执行了呢?把设置HTTP头信息的部分去掉就没事,求解释
* 以Post的方式向服务器提交数据,只返回响应的代码
*
* @param url
* 请求的地址
* @param paramList
* 参数列表
* @return
*/
public static String post(String url, HashMap<String, String> paramList) {
if (url == null) {
throw new NullPointerException();
}
String html = null;
int resultCode = -1;
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>(); int dataSize = paramList.size();
Object[] keys = paramList.keySet().toArray();
for (int i = 0; i < dataSize; i++) {
String key = keys[i].toString();
params.add(new BasicNameValuePair(key, paramList.get(key)));
}
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse get_response = httpClient.execute(request); // 执行请求
HttpPost postMethod = new HttpPost(url);
Header[] headers = get_response.getAllHeaders();// 获取所有的头信息
for (Header header : headers) {
postMethod.addHeader(header.getName(),header.getValue());
}
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); // 将参数填入POST
// Entity中
HttpResponse post_response = httpClient.execute(postMethod); // 执行POST方法
resultCode = post_response.getStatusLine().getStatusCode();// 获取响应代码
System.out.println("相应代码" + resultCode);
if (resultCode == 200) {
html = EntityUtils.toString(post_response.getEntity(), "utf-8");// 获取响应内容
System.out.println(html);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return resultCode;
return html;
}大家看看上面的代码,为什么我在执行post之前设置了HTTP头信息之后就没法执行了呢?把设置HTTP头信息的部分去掉就没事,求解释
解决方案 »
- Android应用开发,ListView滚动条和页面滚动条的冲突问题,棘手
- HTML5 开发 Android应用的问题
- Android animation 加载延时的问题
- 老师:请问,android下的语音识别源码中,哪一部分是从声音(或声音文件)中提取特征参数的?
- 小妹求助TabHost背景图片示例!
- android 4.2 systemUI 透明问题!
- Android通过jdbc的方式怎么连接mongodb数据库啊!!!
- android自启动
- android获取网络图片并显示的问题
- 刚学android不久,请教个ViewPager用法,出错了,不会改,望赐教。。
- sudo apt-get install lib32z1-dev为什么安装不了了!!!
- 绝对定位下ImageView重绘Bitmap的问题
那我可不可设置HttpPost不要自动添加头信息?如果可以该怎么添加呢
哪些头信息不能放在POST里面呢
post本来就是通过消息体封装的