作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我在 android 4.0 中使用此代码时遇到一些问题,我没有得到响应,但在 2.2,2.3,2.3.3 版本中得到了响应
List<NameValuePair> pwadd = new ArrayList<NameValuePair>();
pwadd.add(new BasicNameValuePair("UName", UName));
HttpParams httpParameters = new BasicHttpParams();
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(
"http://10.0.2.2/new/webser/Load.php");
httppost.setEntity(new UrlEncodedFormEntity(pwadd));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
String response = sb.toString();
我应该在这段代码中做什么才能在android 4.0版本中运行?
最佳答案
在异步任务中尝试这些。无论如何,你需要在单独的线程中制作网络内容
public static String PrepareSendPostData_DetailsActivity(String station_id) {
//Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("your url here");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(100);
nameValuePairs.add(new BasicNameValuePair("param_1", "value_1"));
nameValuePairs.add(new BasicNameValuePair("param_2", "ok"));
nameValuePairs.add(new BasicNameValuePair("module", "dbgestion"));
nameValuePairs.add(new BasicNameValuePair("pdv_id", station_id));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String responseBody = getResponseBody(response);
if (responseBody != null)
return responseBody;
else
return null;
} catch (ClientProtocolException e) {
Log.e("exception here", e.getMessage().toString());
return null;
} catch (IOException e) {
Log.e("exception here 2", e.getMessage().toString());
return null;
}
}
public static String getResponseBody(HttpResponse response) {
String response_text = null;
HttpEntity entity = null;
try {
entity = response.getEntity();
response_text = _getResponseBody(entity);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
if (entity != null) {
try {
entity.consumeContent();
} catch (IOException e1) {
}
}
}
return response_text;
}
public static String _getResponseBody(final HttpEntity entity) throws IOException, ParseException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
InputStream instream = entity.getContent();
if (instream == null) {
return "";
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
"HTTP entity too large to be buffered in memory");
}
String charset = getContentCharSet(entity);
if (charset == null) {
charset = HTTP.DEFAULT_CONTENT_CHARSET;
}
Reader reader = new InputStreamReader(instream, charset);
StringBuilder buffer = new StringBuilder();
try {
char[] tmp = new char[1024];
int l;
while ((l = reader.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
} finally {
reader.close();
}
return buffer.toString();
}
public static String getContentCharSet(final HttpEntity entity) throws ParseException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
String charset = null;
if (entity.getContentType() != null) {
HeaderElement values[] = entity.getContentType().getElements();
if (values.length > 0) {
NameValuePair param = values[0].getParameterByName("charset");
if (param != null) {
charset = param.getValue();
}
}
}
return charset;
}
希望对你有帮助...
关于java - 如何在 Android 4.0 中获取 HttpClient 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9817953/
我是一名优秀的程序员,十分优秀!