0

我正在为一个网站开发一个 android 应用程序,该网站由 Nginx 上的 Ruby on Rail 组成,它将提供 HTTP-REST API,并以 JSON 格式响应。很奇怪的是它输出了带有这个 HTTP 响应头的东西,而 android 无法读取它:

Cache-Control   private
Connection  keep-alive
Content-Disposition inline
Content-Length  6608
Etag    "5c6304b4d6f23fab9b841ec3567b32c8"
Server  nginx/1.2.2 + Phusion Passenger 3.0.15 (mod_rails/mod_rack)
Status  304
Vary    User-Agent, Accept-Encoding
X-Powered-By    Phusion Passenger (mod_rails/mod_rack) 3.0.15
X-Runtime   16
content-transfer-encoding   binary

我使用这个函数来获取 JSON - 它在没有内容传输编码二进制文件的其他 API 上运行良好,但是当读取这样的数据时,什么都不会读取。所以我想知道如何在 android 上处理二进制,或者为什么站点输出二进制格式,有什么方法可以在 Ruby on Rail 上处理它吗?谢谢。

public String getJSON(String url) {

        try {
            HttpParams httpParameters = new BasicHttpParams();

            int timeoutConnection = HTTP_TIMEOUT_CONNECTION;
            HttpConnectionParams.setConnectionTimeout(httpParameters,
                    timeoutConnection);

            int timeoutSocket = HTTP_TIMEOUT_SOCKET;
            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

            DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
            HttpPost httpPost = new HttpPost(url);

            httpPost.setHeader("Accept-Encoding","deflate");

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

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

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, Charset.defaultCharset()), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }

            android.util.Log.v ("JSONParser", sb.toString());

            is.close();
            json = sb.toString();
            return json;

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

        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        json = null;
        return null;
    }

它给了我这个错误-

08-06 15:29:46.218: W/System.err(4318): org.json.JSONException: End of input at character 1 of  
08-06 15:29:46.218: W/System.err(4318):     at org.json.JSONTokener.syntaxError(JSONTokener.java:446)
08-06 15:29:46.218: W/System.err(4318):     at org.json.JSONTokener.nextValue(JSONTokener.java:93)
08-06 15:29:46.238: W/System.err(4318):     at org.json.JSONArray.<init>(JSONArray.java:87)
08-06 15:29:46.238: W/System.err(4318):     at org.json.JSONArray.<init>(JSONArray.java:103)
08-06 15:29:46.238: W/System.err(4318):     at com.baozoumanhua.android.network.JSONParser.parseJSONArray(JSONParser.java:181)
08-06 15:29:46.238: W/System.err(4318):     at com.baozoumanhua.android.network.Comments.getJSONArray(Comments.java:176)
08-06 15:29:46.238: W/System.err(4318):     at com.baozoumanhua.android.network.Comments$1.run(Comments.java:151)
08-06 15:29:46.238: W/System.err(4318):     at java.lang.Thread.run(Thread.java:1019)
4

1 回答 1

0

问题解决了,用httpGet代替httpPost...

于 2012-08-06T20:00:07.400 回答