麻烦HTTP GET [英] Trouble with HTTP GET

查看:161
本文介绍了麻烦HTTP GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦一个HTTP GET工作。我曾尝试与OKHttp lib和默认的Andr​​oid HTTP客户端。现在的问题是,我的身体反应,当我使用默认的HTTP客户端收到这个

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 

和这个当我使用okhttp的lib

and this when I use the okhttp lib

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

响应体是假设包含一个JSON对象。如果我粘贴网址在浏览器窗口中JOSN正确返回。我还检查了服务器日志和响应得到正确发送形式传递到服务器。我打电话从一个异步类HTTP GET方法。

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.

使用默认的Andr​​oid客户端的HTTP GET方法:

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

使用OkHTTP LIB GET方法:

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


}

我在做什么错,为什么不能我得到了应用程序的正确反应时,工作完全正常,当我过去相同的URL在浏览器窗口?

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?

让我知道如果你需要看到更多的code。

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

干杯

推荐答案

试试这个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();

    }

这篇关于麻烦HTTP GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆