setListAdapter无法解析

时间:2014-11-29 19:32:40

标签: java android json

真正的业余开发人员急需一些帮助。我一直在尝试解析JSON数据并将其转换为项目列表,但我尝试使用setListAdapter方法时遇到困难,尽管将其导入到类中。任何帮助都会受到大力赞赏。

这是我的主要活动,错误在onPostExecute方法

import java.io.IOException;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;

import android.app.Activity;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;


public class JSON extends Activity {

    // Coordinates used for centering the Map

    private final static String UNAME = "aporter";
    private final static String URL = "http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username="
            + UNAME;

    public static final String TAG = "MapsEarthquakeMapActivity";

    // Set up UI and get earthquake data
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        new HttpGetTask().execute(URL);

    }

    private class HttpGetTask extends
            AsyncTask<String, Void, List<GeonameRec>> {

        AndroidHttpClient mClient = AndroidHttpClient.newInstance("");

        @Override
        protected List<GeonameRec> doInBackground(String... params) {

            HttpGet request = new HttpGet(params[0]);
            JSONResponseHandler responseHandler = new JSONResponseHandler();

            try {

                // Get Earthquake data in JSON format
                // Parse data into a list of EarthQuakeRecs

                return mClient.execute(request, responseHandler);

            } catch (ClientProtocolException e) {
                Log.i(TAG, "ClientProtocolException");
            } catch (IOException e) {
                Log.i(TAG, "IOException");
            }

            return null;

        }

        @Override
        protected void onPostExecute(List<GeonameRec> result) {

            if (null != mClient)
                mClient.close();
            setListAdapter(new ArrayAdapter<String>(
                 JSON.this,
                 R.layout.listitem, result));



        }
    }

}
}

这是我的格式化JSON响应的类。

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.impl.client.BasicResponseHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class JSONResponseHandler implements
    ResponseHandler<List<GeonameRec>> {
@Override
public List<GeonameRec> handleResponse(HttpResponse response)
        throws ClientProtocolException, IOException {
    List<GeonameRec> result = new ArrayList<GeonameRec>();
    String JSONResponse = new BasicResponseHandler()
            .handleResponse(response);
    try {
        JSONObject object = (JSONObject) new JSONTokener(JSONResponse)
                .nextValue();
        JSONArray earthquakes = object.getJSONArray("earthquakes");
        for (int i = 0; i < earthquakes.length(); i++) {
            JSONObject tmp = (JSONObject) earthquakes.get(i);
            result.add(new GeonameRec(
                    tmp.getDouble("lat"),
                    tmp.getDouble("lng")));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return result;
}

}

1 个答案:

答案 0 :(得分:0)

setListAdapter方法位于ListActivity,不在Activity。 要在Activity子类中执行此操作,您应该定义ListView并致电listView.setAdapter