如果有可选参数传递给它,我有一个发送 Httppost 的 JSON Parser 类。
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 和 JellyBean 设备,但不适用于 Froyo 和 Gingerbread。错误信息是:
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 和姜饼是否只支持 httpGet?