DefaultHttpClient无法请求Webservice

时间:2013-10-29 06:43:47

标签: android json web-services httpclient httpconnection

在我的一个应用程序中,有一个用PHP开发的webservice调用。我已经使用通用方法来请求JSON格式的Web服务响应。我编写的方法parseJson()在CommonUtiliy文件中,这是可重用的。我在所有应用程序中使用相同的方法。

现在我的问题是请求响应的parseJson()方法在几天前完成了工作,但现在它根本不能用于某些web服务。我无法获得某些web服务的响应,我得到以下错误。我的回复是空的。

任何一个指南都可以吗?为什么会这样?因为它以前工作。

错误:

10-29 06:40:04.381: W/System.err(3404): JSON Response--->
10-29 06:40:04.381: W/System.err(3404): org.json.JSONException: End of input at character 0 of 
10-29 06:40:04.381: W/System.err(3404):     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
10-29 06:40:04.381: W/System.err(3404):     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
10-29 06:40:04.381: W/System.err(3404):     at org.json.JSONObject.<init>(JSONObject.java:154)
10-29 06:40:04.381: W/System.err(3404):     at org.json.JSONObject.<init>(JSONObject.java:171)
10-29 06:40:04.381: W/System.err(3404):     at com.zeal.peak.adapter.GridAdapter$callLikeWS.doInBackground(GridAdapter.java:381)
10-29 06:40:04.381: W/System.err(3404):     at com.zeal.peak.adapter.GridAdapter$callLikeWS.doInBackground(GridAdapter.java:1)
10-29 06:40:04.381: W/System.err(3404):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-29 06:40:04.381: W/System.err(3404):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-29 06:40:04.381: W/System.err(3404):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-29 06:40:04.381: W/System.err(3404):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-29 06:40:04.381: W/System.err(3404):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-29 06:40:04.393: W/System.err(3404):     at java.lang.Thread.run(Thread.java:856)

请求方法:

  /*
 * Call the Webservice read the Json response and return the response in
 * string.
 */
public static String parseJSON(String p_url) {

    String json = null;
    try {
        // Create a new HTTP Client
        DefaultHttpClient defaultClient = new DefaultHttpClient();

        // Setup the get request
        HttpGet httpGetRequest = new HttpGet(BaseUrl + p_url);
        System.out.println("Request URL--->" + BaseUrl + p_url);
        // Execute the request in the client
        HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
        // Grab the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpResponse.getEntity().getContent(), "UTF-8"));
        json = reader.readLine();
        System.err.println("JSON Response--->" + json);


    } catch (Exception e) {
        // In your production code handle any errors and catch the
        // individual exceptions
        e.printStackTrace();
    }
    return json;
}

编辑:

但是如果我在java文件中使用相同的代码,只是从commonutils文件访问它,那么它的工作正常。如下:

         @Override
    protected String doInBackground(String... params) {

        String m_response = null;
        HttpClient client = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(BaseUrl
                + "notification_list.php?uid=" + m_userID);
        HttpResponse response;
        try {
            response = client.execute(httpget);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                InputStream in = response.getEntity().getContent();
                StringBuilder sb = new StringBuilder();
                String line = "";
                BufferedReader bf = new BufferedReader(
                        new InputStreamReader(in));
                while ((line = bf.readLine()) != null) {
                    sb.append(line);
                }
                m_response = sb.toString();
            }
        } catch (ClientProtocolException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    System.err.println("Response=$^^^^^^^^^^" + m_response);

我在这里得到了成功的回复。不知道为什么会这样。

提前致谢。

1 个答案:

答案 0 :(得分:0)

区别在于:

BufferedReader reader = new BufferedReader(
    new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
json = reader.readLine();

BufferedReader bf = new BufferedReader(new InputStreamReader(in));
while ((line = bf.readLine()) != null) {
    sb.append(line);
}
m_response = sb.toString();

第一行只读取响应的第一行 - bf.readLine()读取直到流结束或行结束(第一行换行);而第二个读取所有行直到结束流 - 所以你有一个完整的JSON响应。您在两种情况下都自己记录响应。你为什么不看看Logcat?

将顶部更改为:

BufferedReader reader = new BufferedReader(new InputStreamReader(
    httpResponse.getEntity().getContent(), "UTF-8"));
StringBuilder sb = new StringBuilder();
String json = null;
while((json = reader.readLine()) != null) {
    sb.append(json);
}
json = sb.toString();

应该做的伎俩