JSON没有在Android上的HTTPPost上返回

时间:2013-11-06 13:15:02

标签: java android json http

我在从Android应用调用简单的JSON Web服务时遇到问题。 .execute()使用200-OK状态成功完成,但是我无法读取任何JSON输出或文本。

为了记录,如果我HttpPost常规网页,如Google.com,我可以阅读并解析所有标记。此外,我可以从设备的浏览器中调用完整的 urlWithParams 字符串,并在浏览器中看到JSON输出。这适用于设备的浏览器:

http://maps.googleapis.com/maps/api/distancematrix/json?origins=Seattle&destinations=San+Francisco&mode=bicycling&language=fr-FR&sensor=false

代码运行时,阅读器始终为空白,而reader.readLine()永远不会运行。返回一个空字符串。如果我将网址更改为Google.com,则会有效并返回17,000个字符。谢谢!

    protected String doInBackground(String... uri) {

    String responseString = null;   

    try {
                //String urlGoogle = "http://google.com";
                //String urlWithParams = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Seattle&destinations=San+Francisco&mode=bicycling&language=fr-FR&sensor=false";
                String urlOnly = "http://maps.googleapis.com/maps/api/distancematrix/json";
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(urlOnly);

                httpPost.setHeader("Accept", "application/json");
                httpPost.setHeader("Content-type", "application/json");

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("origins", "Seattle"));
                nameValuePairs.add(new BasicNameValuePair("destinations", "Cleveland"));
                nameValuePairs.add(new BasicNameValuePair("sensor", "false"));
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpclient.execute(httpPost);

                int status = response.getStatusLine().getStatusCode();

                HttpEntity entity = response.getEntity();
                InputStream is = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                StringBuilder sb = new StringBuilder();

                String line = null;
                try {
                    while ((line = reader.readLine()) != null) {
                        sb.append((line + "\n"));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                responseString = sb.toString();
                }}
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
          } 
        catch (ClientProtocolException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();                
        } 

        return responseString;
    }

3 个答案:

答案 0 :(得分:0)

也许你应该测试其他mime类型而不是application / json。

答案 1 :(得分:0)

1 - 检查您的清单文件是否具有INTERNET权限。

2 - 使用此代码返回数据

  BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
try {
        String inputLine;
    while ((inputLine = reader.readLine()) != null) {
    responseString   += inputLine;
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
   }
 }

答案 2 :(得分:0)

解决!调用JSON页面时的空白返回是由于没有定义代理设置。代理设置在设备上设置但per this post,HttpClient不继承它们。

添加以下行解决了我的问题。代码现在返回JSON。

HttpHost proxy = new HttpHost("172.21.31.239", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);