有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

当我在安卓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();

要在安卓 4.0版本中运行,我应该在这段代码中做些什么


共 (1) 个答案

  1. # 1 楼答案

    在异步任务中尝试这些。无论如何,你需要在一个单独的线程中创建网络内容

    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;
    
    }
    

    希望对你有帮助