我做了一个活动“消息”,它应该从给定的 URL 获取 JSON 数据。我尝试制作一个循环来打印 json 数据,但问题出在其他地方。我正在NullPointerException
使用JSONArray
“json”。
消息类:
public class Messages extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = (TextView) findViewById(R.id.tv_messages);
JSONArray json = JSONfunctions.getJSONfromURL("http://docs.blackberry.com/sampledata.json");
tv.setText(json.length());
}
JSON函数类:
public class JSONfunctions {
public static JSONArray getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONArray jArray = null;
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
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();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag get data string ",
"Error converting result " + e.toString());
}
try {
jArray = new JSONArray(result);
} catch (JSONException e) {
Log.e("log_tag create object ",
"Error parsing data " + e.toString());
}
return jArray;
}
}
错误:
08:11:43.683:http连接错误android.os.NetworkOnMainThreadException 08:11:43.693:错误转换结果java.lang.NullPointerException:lock == null 08:11:43.703:解析数据时出错org.json.JSONException:结束08:11:43.723 的字符 0 处的输入:关闭 VM 08:11:43.733:threadid=1:线程以未捕获的异常退出(组=0x40a70930) 08:11:43.923:ATAL 异常:主 08:11:43.923 :java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.sound/com.example.sound.Messages}:java.lang.NullPointerException 08:11:43.923:在android.app.ActivityThread.performLaunchActivity(ActivityThread。 java:2180) 08:11:43.923: 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 08:11:43.923: 在 android.app.ActivityThread.access$600(ActivityThread.java:141) 08:11 :43.923: 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 08:11:43.923: 在 android.os.Handler.dispatchMessage(Handler.java:99) 08:11:43.923: 在 android.os .Looper.loop(Looper.java:137) 08:11:43.923: 在 android.app.ActivityThread.main(ActivityThread.java:5039) 08:11:43.923: 在 java.lang.reflect.Method.invokeNative(Native方法) 08:11:43.923: 在 java.lang.reflect.Method.invoke(Method.java:511) 08:11:43.923: 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 793) 08:11:43.923: 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 08:11:43.923: 在 dalvik.system.NativeStart.main(本机方法) 08:11:43.923 :引起:java.lang.NullPointerException 08:11:43.923:在 com.example.sound.Messages.onCreate(Messages.java:18) 08:11:43.923:在 android.app.Activity。performCreate(Activity.java:5104) 08:11:43.923: 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 08:11:43.923: 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
第 18 行是:
tv.setText(json.length());
在出现上述错误之前,我也得到了很多:
Unexpected value from nativeGetEnabledTags: 0
我正在使用的 URL(出于测试目的,http://docs.blackberry.com/sampledata.json)已启动并正常工作。我是 Android 开发和 JSON 的新手。提前致谢。