我想从Web服务获取json对象并在列表视图中显示它们,
这是我的代码:
public class MainActivity extends Activity {
private JsonObject data = null;
private JsonObject response = null;
private JsonArray records = null;
private JsonObject record = null;
private ArrayList<SearchResults> results = null;
SearchResults sr1 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<SearchResults> searchResults = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.ListView01);
lv1.setAdapter(new MyCustomBaseAdapter(this, searchResults));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private ArrayList<SearchResults> GetSearchResults() {
results = new ArrayList<SearchResults>();
new GetResults().execute("");
return results;
}
private class GetResults extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
Map<String, String> callArgs = new HashMap<String, String>(1);
callArgs.put("suuid", "dtr0bdQGcqwSh3QO7fVwgVfBNWog6mvEbAyljlLX9E642Yfmur");
try {
response = EventPulseCloud.call("ListEvents", callArgs);
} catch (HttpClientException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JsonException e) {
e.printStackTrace();
}
return response.get("Type").toString();
}
protected void onPostExecute(String result) {
if(result.equals("success")) {
data = (JsonObject) response.get("Data");
records = (JsonArray) data.get("Records");
try {
records = response.getObject ("Data").getArray ("Records");
} catch (JsonException e) {
e.printStackTrace();
}
for(int i = 0; i < records.count(); i ++) {
record = (JsonObject) records.get(i);
sr1 = new SearchResults();
sr1.setAddress(record.get("address").toString());
sr1.setStartingDate(record.get("StartingDate").toString());
sr1.setTicketCategories(record.get("ticketCategories").toString());
results.add(sr1);
}
}
}
}
}
我在logcat中得到了这个:
06-05 15:53:39.765: E/AndroidRuntime(10012): FATAL EXCEPTION: AsyncTask #1
06-05 15:53:39.765: E/AndroidRuntime(10012): java.lang.RuntimeException: An error occured while executing doInBackground()
06-05 15:53:39.765: E/AndroidRuntime(10012): at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-05 15:53:39.765: E/AndroidRuntime(10012): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-05 15:53:39.765: E/AndroidRuntime(10012): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-05 15:53:39.765: E/AndroidRuntime(10012): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-05 15:53:39.765: E/AndroidRuntime(10012): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-05 15:53:39.765: E/AndroidRuntime(10012): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-05 15:53:39.765: E/AndroidRuntime(10012): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-05 15:53:39.765: E/AndroidRuntime(10012): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-05 15:53:39.765: E/AndroidRuntime(10012): at java.lang.Thread.run(Thread.java:856)
06-05 15:53:39.765: E/AndroidRuntime(10012): Caused by: java.lang.NullPointerException
06-05 15:53:39.765: E/AndroidRuntime(10012): at com.example.listview.MainActivity$GetResults.doInBackground(MainActivity.java:73)
06-05 15:53:39.765: E/AndroidRuntime(10012): at com.example.listview.MainActivity$GetResults.doInBackground(MainActivity.java:1)
06-05 15:53:39.765: E/AndroidRuntime(10012): at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-05 15:53:39.765: E/AndroidRuntime(10012): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-05 15:53:39.765: E/AndroidRuntime(10012): ... 5 more
06-05 15:53:40.050: I/Process(10012): Sending signal. PID: 10012 SIG: 9
在logcat中,行: MainActivity.java:73 是:
return response.get("Type").toString();
对我来说一切似乎都是正确的......拜托,你对此有什么想法吗?谢谢:))
编辑: 当我在java中尝试这段代码时:
Map<String, String> callArgs = new HashMap<String, String>(1);
callArgs.put("suuid", "dtr0bdQGcqwSh3QO7fVwgVfBNWog6mvEbAyljlLX9E642Yfmur");
response = EventPulseCloud.call("ListEvents", callArgs);
if(response.get("Type").equals("success")) {
JsonArray records = response.getObject ("Data").getArray ("Records");
for (int i = 0; i < records.count (); i++) {
JsonObject record = (JsonObject)records.get (i);
System.out.print(record.get("ticketCategories").toString());
}
}
else
System.out.println("No");
并显示在屏幕上:
[&#34; SingleDay&#34;] [&#34; SingleDay&#34;] [&#34;普通票&#34;]
它确实意味着它会返回一些东西。
答案 0 :(得分:1)
我猜测response.get("Type")
的结果为空。在尝试使用它之前,你应该检查它的返回。
答案 1 :(得分:1)
response.get("Type")
返回null。确保你正在寻找正确的东西(也许它是“类型”而不是“类型”?),如果没有别的,添加一些空值检查。
Object typeObj = response.get("Type");
if (typeObj != null) {
return typeObj.toString();
}
return null;
答案 2 :(得分:0)
也许缺少访问Internet的许可请求。避免它。在Android清单文件中添加
<uses-permission android:name="android.permission.INTERNET" />