- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我做了一个 Activity “消息”,它应该从给定的 URL 获取 JSON 数据。我尝试制作一个循环来打印 json 数据,但问题出在其他地方。我在 JSONArray
,“json”上收到 NullPointerException
。
消息类:
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: Error in http connection android.os.NetworkOnMainThreadException 08:11:43.693: Error converting result java.lang.NullPointerException: lock == null 08:11:43.703: Error parsing data org.json.JSONException: End of input at character 0 of 08:11:43.723: Shutting down VM 08:11:43.733: threadid=1: thread exiting with uncaught exception (group=0x40a70930) 08:11:43.923: ATAL EXCEPTION: main 08:11:43.923: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sound/com.example.sound.Messages}: java.lang.NullPointerException 08:11:43.923: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 08:11:43.923: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 08:11:43.923: at android.app.ActivityThread.access$600(ActivityThread.java:141) 08:11:43.923: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 08:11:43.923: at android.os.Handler.dispatchMessage(Handler.java:99) 08:11:43.923: at android.os.Looper.loop(Looper.java:137) 08:11:43.923: at android.app.ActivityThread.main(ActivityThread.java:5039) 08:11:43.923: at java.lang.reflect.Method.invokeNative(Native Method) 08:11:43.923: at java.lang.reflect.Method.invoke(Method.java:511) 08:11:43.923: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 08:11:43.923: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 08:11:43.923: at dalvik.system.NativeStart.main(Native Method) 08:11:43.923: Caused by: java.lang.NullPointerException 08:11:43.923: at com.example.sound.Messages.onCreate(Messages.java:18) 08:11:43.923: at android.app.Activity.performCreate(Activity.java:5104) 08:11:43.923: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 08:11:43.923: at 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 的新手。提前致谢。
最佳答案
这是因为要么你正在主线程上执行网络操作,而 Android 版本 >= 3.0 不允许。
解决这个问题要么使用
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
或
Use AsyncTask
阅读自http://developer.android.com/reference/android/os/AsyncTask.html
关于android - getJSONfromURL - 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14670677/
我做了一个 Activity “消息”,它应该从给定的 URL 获取 JSON 数据。我尝试制作一个循环来打印 json 数据,但问题出在其他地方。我在 JSONArray,“json”上收到 Nul
是否可以为 GetJsonFromUrl 辅助方法设置超时?或者如果我需要默认的 30 秒超时以外的东西,我应该切换到 JsonServiceClient 吗? 最佳答案 您可以在所有 HTTP Ut
我是一名优秀的程序员,十分优秀!