Trouble with HTTP GET

2019-08-27 20:05发布

问题:

I am having some trouble getting a HTTP GET to work. I have tried with the OKHttp lib and the default android HTTP client. The problem is that I am getting this in the response body when I use the default http client

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

and this when I use the okhttp lib

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

The response body is suppose to contain a JSON object. If I paste the URL in a browser window the JOSN is returned correctly. I have also checked the server logs and the response is getting sent correctly form the server. I am calling the HTTP GET method from an async class.

The GET method using the default Android HTTP client:

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

The GET method using the OkHTTP lib:

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


}

What am I doing wrong, why cant I get the correct response from the app when it works perfectly fine when I past the same url in a browser window?

Let me know if you need to see any more of the code.

Cheers

回答1:

Try this code :

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();

    }