【问题标题】:Trouble with HTTP GETHTTP GET 的问题
【发布时间】:2014-08-24 17:23:15
【问题描述】:

我在让 HTTP GET 工作时遇到了一些麻烦。我尝试过使用 OKHttp 库和默认的 android HTTP 客户端。问题是当我使用默认的 http 客户端时,我在响应正文中得到了这个

org.apache.http.conn.BasicManagedEntity@424ab550 

当我使用 okhttp 库时

com.squareup.okhttp.Call$RealResponseBody@424a6c58 

假设响应正文包含一个 JSON 对象。如果我将 URL 粘贴到浏览器窗口中,则 JOSN 会正确返回。我还检查了服务器日志,并且响应从服务器正确发送。我正在从异步类调用 HTTP GET 方法。

使用默认 Android HTTP 客户端的 GET 方法:

public String getData(String url)
{

    Log.w("Rakshak", "in the get Data data method");

    Log.w("Rakshak", "URL: "+url);

    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 20000000); //Timeout Limit
    HttpResponse httpResponse;
    try {
            Log.w("Rakshak", "in the get data try");
            HttpGet get = new HttpGet(url);
            httpResponse = client.execute(get);
            String response = httpResponse.getEntity().toString();
            Log.w("Rakshak", "Get responce: "+response); // this posts "org.apache.http.conn.BasicManagedEntity@424ab550" insted of the JSON that am supposed to get
            return response;

        } catch(Exception e) 
        {
                e.printStackTrace();

        }
    return "1";
}

使用 OkHTTP 库的 GET 方法:

public String getData(String url)
{
    OkHttpClient client = new OkHttpClient();

    //Log.w("Rakshak", "in the get Data data method ");

    //Log.w("Rakshak", "URL: "+url);

    Request request = new Request.Builder()
                                 .url(url)
                                 .build();

    try {
        Response response = client.newCall(request).execute();

        response.toString();


        Log.w("Rakshak", " in the service handeler"+response.body().toString()); // this posts "com.squareup.okhttp.Call$RealResponseBody@424a6c58" insted of the JSON that I supposed to get in the body.

        return response.body().toString();

    } catch (IOException e) {           
        e.printStackTrace();

        return "failed";
    }


}

我做错了什么,当我在浏览器窗口中通过相同的 url 时,为什么我无法从应用程序中获得正确的响应?

如果您需要查看更多代码,请告诉我。

干杯

【问题讨论】:

    标签: android http-get


    【解决方案1】:

    试试这个代码:

    class PlaceOrder extends AsyncTask<Void, Void, Void> {
    
            @Override
            protected Void doInBackground(Void... params) {
    
                // TODO Auto-generated method stub
    
                try {
    
                    HttpClient httpClient = new DefaultHttpClient();
    
                    HttpPost httpPst = new HttpPost(
    
                    "yout_url");
    
                    ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>(
    
                    2);
    
                    parameters.add(new BasicNameValuePair("username", "apple"));
    
                    parameters.add(new BasicNameValuePair("pw", "apple"));
    
                    parameters.add(new BasicNameValuePair("email",
                            "apple@gmail.com"));
    
                    parameters.add(new BasicNameValuePair("name", "apple"));
    
                    httpPst.setEntity(new UrlEncodedFormEntity(parameters));
    
                    HttpResponse httpRes = httpClient.execute(httpPst);
    
                    String str = convertStreamToString(
                            httpRes.getEntity().getContent()).toString();
    
                    Log.i("mlog", "outfromurl" + str);
    
                } catch (UnsupportedEncodingException e) {
    
                    // TODO Auto-generated catch block
    
                    e.printStackTrace();
    
                } catch (ClientProtocolException e) {
    
                    // TODO Auto-generated catch block
    
                    e.printStackTrace();
    
                } catch (IOException e) {
    
                    // TODO Auto-generated catch block
    
                    e.printStackTrace();
    
                }
    
                return null;
    
            }
    
        }
    
        public static String convertStreamToString(InputStream is) {
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    
            StringBuilder sb = new StringBuilder();
    
            String line = null;
    
            try {
    
                while ((line = reader.readLine()) != null) {
    
                    sb.append(line + "\n");
    
                }
    
            } catch (Exception e) {
    
                e.printStackTrace();
    
            } finally {
    
                try {
    
                    is.close();
    
                } catch (IOException e) {
    
                    e.printStackTrace();
    
                }
    
            }
    
            return sb.toString();
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-25
      • 2011-06-12
      • 2019-01-08
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      相关资源
      最近更新 更多