2

我想从 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");

它显示在屏幕上:

["SingleDay"] ["SingleDay"] ["普通票"]

它确实意味着它返回了一些东西。

4

3 回答 3

1

我猜结果response.get("Type")是空的。在尝试用它做某事之前,你应该真正检查它的返回。

于 2013-06-05T15:09:27.393 回答
1

response.get("Type")正在返回 null。确保您正在寻找正确的东西(也许它是“类型”而不是“类型”?),如果没有别的,添加一些空检查。

Object typeObj = response.get("Type");
if (typeObj != null) {
    return typeObj.toString();
}
return null;
于 2013-06-05T15:09:36.670 回答
0

也许缺少访问互联网的许可请求。避免它。在 Android 清单文件中添加

<uses-permission android:name="android.permission.INTERNET" />
于 2018-07-01T19:29:55.407 回答