我一直潜伏在这个网站已有一段时间了,幸运的是直到现在我所有的问题都得到了解答。我希望你们中的一些人可以在这里阐明我的问题。我可以成功地获取ListView以显示字符串数组 - 但是当我尝试将JSON数据解析为数组时 - 我的程序只是强制关闭而LogCat对我没什么帮助。 Anywho - 这是我正在使用的代码。
String[] journals = {"Filler"};
String result;
JSONArray jArray[];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
//http post
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxx.xxx.xxx.xxx/query.php");
org.apache.http.HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
//convert response to string
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();
//I think the problem is somewhere here ....
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
journals[i] = json_data.getString("JournalName");
Log.i("log_tag","JournalID: "+json_data.getInt("JournalID") +
",JournalName: " + json_data.getString("JournalName"));
}
}
catch (Exception e) {
}
//Or here -- I know this code should REALLY be separated and not in onCreate -- I feel like that's half my problem
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, journals));
ListView lv = getListView();
先谢谢你们!我很感激。
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): FATAL EXCEPTION: main
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): java.lang.RuntimeException: Unable to start activity ComponentInfo{Andrew.Badura.A4A.com/Andrew.Badura.A4A.com.SurvAcademia}: java.lang.RuntimeException: Unable to start activity ComponentInfo{Andrew.Badura.A4A.com/Andrew.Badura.A4A.com.JournalActivity}: java.lang.NullPointerException
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.os.Looper.loop(Looper.java:123)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at java.lang.reflect.Method.invoke(Method.java:521)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at dalvik.system.NativeStart.main(Native Method)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{Andrew.Badura.A4A.com/Andrew.Badura.A4A.com.JournalActivity}: java.lang.NullPointerException
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.widget.TabHost.addTab(TabHost.java:216)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at Andrew.Badura.A4A.com.SurvAcademia.onCreate(SurvAcademia.java:25)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): ... 11 more
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): Caused by: java.lang.NullPointerException
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at java.util.Arrays$ArrayList.<init>(Arrays.java:49)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at java.util.Arrays.asList(Arrays.java:171)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:125)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at Andrew.Badura.A4A.com.JournalActivity.onCreate(JournalActivity.java:69)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-07 22:24:33.060: ERROR/AndroidRuntime(7986): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
答案 0 :(得分:1)
这个代码在如此多的级别上都是错误的,从忽略Java约定开始,完全吞噬了异常处理。
我强烈建议您在应用的主线程中执行任何类型的远程调用。您需要将其至少包装到AsyncTask(或可能是Service)中,并使用回调填充ListView以确保您返回到主UI线程
答案 1 :(得分:1)
你可以使用一个开放的API Gson.jar从Json获取数据..它以标准方式获取数据...试试它的例子...... http://blog.foos-bar.com/2010/08/parsing-facebook-json.html