2017-03-02 9 views
0

Ich mache eine mobile App, die Informationen von meiner eigenen API abruft. Ich versuche, ein Restaurant Details in JSON zu bekommen und sie zu analysieren, um angezeigt zu werden. hier ist der Fehler Ich erhalte:JSON Parsing Error kein Wert für Variable

D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1 
I/Timeline: Timeline: Activity_idle id: [email protected] time:148477558 
E/MainActivity: Response from url: { 
       "address1": "Market Square, Smithfield, Dublin Dublin 7", 
       "address2": "Dublin 7", 
       "cost": 35, 
       "lat": 53.3489980000, 
       "lng": -6.2788120000, 
       "menu_type": "BBQ", 
       "name": "My Meat Wagon", 
       "offer": "Meal for 10\u20ac", 
       "phone": 53463267, 
       "rate": 4.1 
      } 
E/MainActivity: Json parsing error: No value for restaurants 
D/ViewRootImpl: #3 mView = null 

und hier ist der Code Ich verwende:

protected Void doInBackground(Void... arg0) { 
     HttpHandler sh = new HttpHandler(); 

     // Making a request to url and getting response 
     String jsonStr = sh.makeServiceCall(url); 

     Log.e(TAG, "Response from url: " + jsonStr); 

     if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 

       // Getting JSON Array node 
       JSONArray restaurants = jsonObj.getJSONArray("restaurants"); 

       // looping through the JSON object 
        JSONObject c = restaurants.getJSONObject(0); 

        String name = c.getString("name"); 
        String address1 = c.getString("address1"); 
        String address2 = c.getString("address2"); 
        String lat = c.getString("lat"); 
        String lng = c.getString("lng"); 
        String cost = c.getString("cost"); 
        String menu_type = c.getString("menu_type"); 
        String rate = c.getString("rate"); 
        String offer = c.getString("offer"); 


        // Phone node is JSON Object 
        String mobile = c.getString("mobile"); 
        // tmp hash map for single restaurant 
        HashMap<String, String> restaurant = new HashMap<>(); 

        // adding each child node to HashMap key => value 
        restaurant.put("name", name); 
        restaurant.put("address1", address1); 
        restaurant.put("address2", address2); 
        restaurant.put("lat", lat); 
        restaurant.put("lng", lng); 
        restaurant.put("cost", cost); 
        restaurant.put("menu_type", menu_type); 
        restaurant.put("rate", rate); 
        restaurant.put("offer", offer); 
        restaurant.put("mobile", mobile); 

        contactList.add(restaurant); 

      } catch (final JSONException e) { 
       Log.e(TAG, "Json parsing error: " + e.getMessage()); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Toast.makeText(getApplicationContext(), 
           "Json parsing error: " + e.getMessage(), 
           Toast.LENGTH_LONG) 
           .show(); 
        } 
       }); 

      } 
     } else { 
      Log.e(TAG, "Couldn't get json from server."); 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        Toast.makeText(getApplicationContext(), 
          "Couldn't get json from server. Check LogCat for possible errors!", 
          Toast.LENGTH_LONG) 
          .show(); 
       } 
      }); 

     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     // Dismiss the progress dialog 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
     /** 
     * Updating parsed JSON data into ListView 
     * */ 
     ListAdapter adapter = new SimpleAdapter(
       MainActivity.this, contactList, 
       R.layout.list_item, new String[]{"name", "address1", 
       "address2","lat","lng","menu_type","Phone","rate","offer","cost"}, new int[]{R.id.name, 
       R.id.address1, R.id.address2,R.id.lat,R.id.lng,R.id.menu,R.id.mobile,R.id.rate,R.id.offer,R.id.cost}); 

     lv.setAdapter(adapter); 
    } 

} 
    } 
+0

Das JSON-Objekt, das Sie drucken, 'jsonStr', hat keinen Knoten namens" restaurants ". –

+0

"Kein Wert für Restaurants" Es gibt keinen solchen Schlüssel in Ihrem JSON-Objekt, also was erwarten Sie? – njzk2

Antwort

0

würde ich vorschlagen, Google Gson Bibliothek verwenden json Strings zum Parsen.

Im Grunde müssen Sie die Bibliothek importieren, erstellen Sie entsprechende POJO und rufen Sie gson.fromJson (jsonStringToParseFrom, YourPOJOClassName.class). So wird Ihre doInBackground wie folgt aussehen:

protected Void doInBackground(Void... arg0) { 
    HttpHandler sh = new HttpHandler(); 

    // Making a request to url and getting response 
    String jsonStr = sh.makeServiceCall(url); 

    Gson gson = new Gson(); 
    YourPOJOClass parsedResponse = gson.fromJson(jsonStr, YourPOJOClass.class); 

Wenn es einige Parsen Probleme auf sein Gson Sie gut informiert Ausnahme bekommen, was schief gelaufen ist, und Sie werden alle Text Parsing-Code abschütteln.