当我执行下面给出的代码时,它向我显示了一个异常,例如
“JSON Parser:解析数据 org.json.JSONException 时出错:org.json.JSONObject$1 类型的值为 null 无法转换为 JSONObject”
这里有人可以帮助我吗?
public class JSONParser {
static JSONArray jarray = new JSONArray();
static JSONObject jobj = new JSONObject();
public JSONArray getJSONFromUrl(String url) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e("==>", "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Parse String to JSON object
try {
jobj = new JSONObject(String.valueOf(builder));
jarray = new JSONArray(jobj);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON Object
return jarray;
}
}
我试图从服务器获取的数据是
{"eventid":"1","name":"Hacking","text":"6th-7th feb.Faculty i3India tech","date":"6th-7th","time":"9","venue":"MMH","contact":"9033637371","fee":"800"}