java - 如何在 Android 4.0 中获取 HttpClient 的响应

标签 java android httpclient android-4.0-ice-cream-sandwich

当我在 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/

相关文章:

c# - 如何使用 HttpClient.PostAsync 发送 XML 内容?

java - JSP Struts 到 Velocity Struts 的转换

java - 使用 Java App Engine 的多个站点

java - Linux 和 Mac 上的免费 Java native 启动器

java - 更改 nav_header_main 中的背景

android - 如何指定android :listSelector ="@null" programmatically?

java - httpclient 可以使用当前登录用户自动创建授权 header 作为 java URLConnection 吗?

java - Apache 通用编解码器 - 为什么 Hex.encode() 不是静态的?

android - 项目级或全局 build.gradle 中的通用 gradle 配置

c# - 从数据库接收数据并将其转换为字典