我有一個JSON解析器類,如果有傳遞給它的可選參數,它會發送一個Httppost。json解析器不工作在froyo和薑餅,但在ics上工作
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, Object... params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
if(params.length > 0){
httpPost.setEntity(new UrlEncodedFormEntity((List<? extends NameValuePair>)params[0]));
}
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, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
此作品在我的ICS和豆形軟糖設備,但不升級Froyo和薑餅。 的錯誤信息是:
10-21 13:49:10.348: E/Buffer Error(471): Error converting result java.lang.NullPointerException
10-21 13:49:10.348: E/JSON Parser(471): Error parsing data org.json.JSONException: End of input at character 0 of
有人可以幫助我嗎? froyo和gingerbread只支持httpGet?
10-21 13:49:10.348:E/JSON解析器(471):錯誤的數據進行解析組織。 json.JSONException:在字符0處輸入結束。這是錯誤信息。我可以在ICS和軟糖豆豆中使用這個解析器,但不能用在froyo和gingerbread設備中 – JHoo
哦,看起來你可能不得不採用另一種方式 – kabuto178