0

Foursquare Venue List を取得しようとしています。Json を解析する非同期クラスと、リストを作成して表示する別のクラスがあります。しかし、解析エラー、nullpointerexception などのエラーがいくつかあります。これを修正する方法がわかりません。解析したいJsonは、フォースクエアのURLから来ています。たとえば、これhttps://api.foursquare.com/v2/venues/search?ll=40.7,-74&radius=0&client_id=50QO00GZKUPA25PDGO1IY3UHMK3IBEMO2F4CTO5M2DBLVGQD&client_secret=MVJZGC3WT4V55DQPJYBFNZ5X3IACIDLFVM1HKA2WBTEHAXDF3&v

ここにlogCatがあります

07-24 12:39:06.485: E/SensorManager(19711): thread start
07-24 12:39:06.495: D/SensorManager(19711): registerListener :: handle = 0  name= K3DH Acceleration Sensor delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)8e(:e8
07-24 12:39:10.455: E/fail!(19711): Error convert stream to String: java.lang.NullPointerException
07-24 12:39:10.455: E/fail!(19711): Error convert stream to String: java.lang.NullPointerException
07-24 12:39:10.455: E/JSON Parser(19711): Error parsing data org.json.JSONException: End of input at character 0 of 
07-24 12:39:10.455: E/JSON Parser(19711): Error parsing data org.json.JSONException: End of input at character 0 of 
07-24 12:39:10.455: D/AndroidRuntime(19711): Shutting down VM
07-24 12:39:10.455: W/dalvikvm(19711): threadid=1: thread exiting with uncaught exception (group=0x41e7f2a0)
07-24 12:39:10.460: E/AndroidRuntime(19711): FATAL EXCEPTION: main
07-24 12:39:10.460: E/AndroidRuntime(19711): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mykong.android/com.mykong.android.JSonResultActivity}: java.lang.NullPointerException
07-24 12:39:10.460: E/AndroidRuntime(19711):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
07-24 12:39:10.460: E/AndroidRuntime(19711):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
07-24 12:39:10.460: E/AndroidRuntime(19711):    at android.app.ActivityThread.access$700(ActivityThread.java:140)
07-24 12:39:10.460: E/AndroidRuntime(19711):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
07-24 12:39:10.460: E/AndroidRuntime(19711):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 12:39:10.460: E/AndroidRuntime(19711):    at android.os.Looper.loop(Looper.java:137)
07-24 12:39:10.460: E/AndroidRuntime(19711):    at android.app.ActivityThread.main(ActivityThread.java:4921)
07-24 12:39:10.460: E/AndroidRuntime(19711):    at java.lang.reflect.Method.invokeNative(Native Method)
07-24 12:39:10.460: E/AndroidRuntime(19711):    at java.lang.reflect.Method.invoke(Method.java:511)
07-24 12:39:10.460: E/AndroidRuntime(19711):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
07-24 12:39:10.460: E/AndroidRuntime(19711):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
07-24 12:39:10.460: E/AndroidRuntime(19711):    at dalvik.system.NativeStart.main(Native Method)
07-24 12:39:10.460: E/AndroidRuntime(19711): Caused by: java.lang.NullPointerException
07-24 12:39:10.460: E/AndroidRuntime(19711):    at com.mykong.android.JSonResultActivity.onCreate(JSonResultActivity.java:65)
07-24 12:39:10.460: E/AndroidRuntime(19711):    at android.app.Activity.performCreate(Activity.java:5206)
07-24 12:39:10.460: E/AndroidRuntime(19711):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-24 12:39:10.460: E/AndroidRuntime(19711):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
07-24 12:39:10.460: E/AndroidRuntime(19711):    ... 11 more

ここに私のパーサーがあります

    public class BackgroundTask extends AsyncTask<String, Void, JSONObject> {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // variables passed in:
    String url;
    String method;
    List<NameValuePair> params;

    // constructor
    public BackgroundTask(String url, String method, 
        List<NameValuePair> params) {
        this.url = url;
        this.method = method;
        this.params = params;
    }


    @Override
    protected JSONObject doInBackground(String... args) {
        try {
            if(method == "POST"){
                DefaultHttpClient httpClient = new DefaultHttpClient();
                httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
                        CookiePolicy.BROWSER_COMPATIBILITY);
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            } else if(method == "GET"){
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        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();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("fail!", "Error convert stream to String: " + e.toString());
        }

        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        return jObj;
    }

    @Override
    protected void onPostExecute(JSONObject obj) {

    }       
}

そして、ここにリストを表示するアクティビティがあります

    public class JSonResultActivity extends ListActivity {


// déclarer l’URL du fichier employes.json
private static String url = "https://api.foursquare.com/v2/venues/search?ll=40.7,-74&radius=0&client_id=50QO00GZKUPA25PDGO1IY3UHMK3IBEMO2F4CTO5M2DBLVGQD&client_secret=MVJZGC3WT4V55DQPJYBFNZ5X3IACIDLFVM1HKA2WBTEHAXDF&v=20130703";
private static final String TAG_VENUES = "venues";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_LOCATION = "location";
private static final String TAG_ADDRESS = "adresse";
private static final String TAG_CROSS_STREET = "crossStreet";
private static final String TAG_CITY = "city";
private static final String TAG_STATE = "state";
private static final String TAG_POSTAL_CODE = "postalCode";
private static final String TAG_COUNTRY = "country";
private static final String TAG_LAT = "lat";
private static final String TAG_LNG = "lng";
private static final String TAG_DISTANCE = "distance";



// initialiser à null notre liste d’employés (de type JSONArray)
JSONArray venues = null;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// création de la ArrayList qui nous permettra de remplir la listView
ArrayList<HashMap<String, String>> venuesList = new ArrayList<HashMap<String, String>>();

// instancier la classe JsonParser
//JsonParser jParser = new JsonParser ();

BackgroundTask backgroundTask = new BackgroundTask(null, null, null);
backgroundTask.execute(url);

// récupérer le JSONObject à partir de l’url
//JSONObject json = jParser.getJSONFromUrl(url);
JSONObject json = backgroundTask.doInBackground(url);

try {

// récupérer la liste de tous les employés
venues = json.getJSONArray(TAG_VENUES);

// parcourir toute la liste des employés
for(int i = 0; i < venues.length(); i++){

// récupérer un employé de type JSONObject
JSONObject ven = venues.getJSONObject(i);

// stocker chaque item du JSONObject employe dans une variable de type String
String id = ven.getString(TAG_ID);
String name = ven.getString(TAG_NAME);


// récupérer le JSONObject phone qui contient deux items
JSONObject location = ven.getJSONObject(TAG_LOCATION);
String address = location.getString(TAG_ADDRESS);
String crossStreet= location.getString(TAG_CROSS_STREET);
String city= location.getString(TAG_CITY);
String state= location.getString(TAG_STATE);
String postalCode= location.getString(TAG_POSTAL_CODE);
String country= location.getString(TAG_COUNTRY);
String lat= location.getString(TAG_LAT);
String lng= location.getString(TAG_LNG);
String distance= location.getString(TAG_DISTANCE);

// créer une HashMap pour ajouter les informations dans chaque item
HashMap<String, String> map = new HashMap<String, String>();

// insérer dans la HashMap les données que l’on veut afficher sous la forme de clé/valeur
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_CITY, city);
map.put(TAG_LAT, lat);
map.put(TAG_LNG, lng);
map.put(TAG_DISTANCE, distance);

// ajouter une HashMap à notre ArrayList
venuesList.add(map);
}

} catch (JSONException e) 
{
e.printStackTrace();
}

//créer un SimpleAdapter qui se chargera de placer les données dans chaque item de notre listView
ListAdapter adapter = new SimpleAdapter(this, venuesList,R.layout.activity_json_result, new String[] { TAG_NAME, TAG_ADDRESS, TAG_LAT, TAG_LNG }, 
        new int[] {R.id.name, R.id.address, R.id.lat, R.id.lng });

// attribuer à notre listView l'adapter que l'on vient de créer
setListAdapter(adapter);
}
}

どうすればこれを解決できますか?

ご協力ありがとうございました。

4

0 に答える 0