0
我正在一個網站的Android應用程序,該網站是由Nginx上的Ruby on Rail它將提供HTTP-REST API,並且以JSON格式迴應。很奇怪的是,它輸出一些與此HTTP響應頭和Android無法閱讀:Android:如何處理與「內容傳輸編碼二進制」的HTTP日期
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)
你說的Android無法閱讀。你可以說得更詳細點嗎?你得到什麼樣的錯誤?我們可以得到一些日誌信息嗎? – DallaRosa 2012-08-06 07:33:20
感謝您的快速幫助,它實際上沒有輸出,並給我錯誤(我在上面的帖子中添加) – Tom 2012-08-06 07:38:32
我沒有把解析json代碼放在這裏,它會解析json,所以如果字符串是空的,將什麼也不給,但是這個錯誤? – Tom 2012-08-06 07:39:27