2017-07-16 2 views
0

Ich erhalte Fehler:Android: java.lang.String kann nicht in JSONObject umgewandelt wird

java.lang.String cannot be converted to JSONObject

ich testen möchte dieses Tutorial auf dieser Website (http://www.androidhive.info/2012/01/android-json-parsing-tutorial/), aber ich habe einen Fehler (Werte vom Typ Java .lang.String kann nicht in JSONObject umgewandelt werden), vielen dank für Hilfe

public class MainActivity extends AppCompatActivity { 

private String TAG = MainActivity.class.getSimpleName(); 

private ProgressDialog pDialog; 
private ListView lv; 

// URL to get contacts JSON 
private static String url = "http://api.androidhive.info/contacts/"; 

ArrayList<HashMap<String, String>> contactList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    contactList = new ArrayList<>(); 

    lv = (ListView) findViewById(R.id.list); 

    new GetContacts().execute(); 
} 

/** 
* Async task class to get json by making HTTP call 
*/ 
private class GetContacts extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Showing progress dialog 
     pDialog = new ProgressDialog(MainActivity.this); 
     pDialog.setMessage("Please wait..."); 
     pDialog.setCancelable(false); 
     pDialog.show(); 

    } 

    @Override 
    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 contacts = jsonObj.getJSONArray("contacts"); 

       // looping through All Contacts 
       for (int i = 0; i < contacts.length(); i++) { 
        JSONObject c = contacts.getJSONObject(i); 

        String id = c.getString("id"); 
        String name = c.getString("name"); 
        String email = c.getString("email"); 
        String address = c.getString("address"); 
        String gender = c.getString("gender"); 

        // Phone node is JSON Object 
        JSONObject phone = c.getJSONObject("phone"); 
        String mobile = phone.getString("mobile"); 
        String home = phone.getString("home"); 
        String office = phone.getString("office"); 

        // tmp hash map for single contact 
        HashMap<String, String> contact = new HashMap<>(); 

        // adding each child node to HashMap key => value 
        contact.put("id", id); 
        contact.put("name", name); 
        contact.put("email", email); 
        contact.put("mobile", mobile); 

        // adding contact to contact list 
        contactList.add(contact); 
       } 
      } 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", "email", 
       "mobile"}, new int[]{R.id.name, 
       R.id.email, R.id.mobile}); 

     lv.setAdapter(adapter); 
    } 

} 
} 
+0

überprüfen Sie Ravi Tamadas neueste Tutorials auf derselben Website für JSON Parsing mit Volley und machen Sie Ihr Leben leichter. – deejay

+0

Welche Ausnahme bekommen Sie? poste deinen Logcat – Jayanth

+0

Es wäre hilfreich, wenn du gerade genug hinzufügen könntest, um das Problem zu reproduzieren. Siehe: [Erstellen eines minimalen, vollständigen und überprüfbaren Beispiels] (https://stackoverflow.com/help/mcve) –

Antwort

0

ändern Sie die URL zu "https://api.androidhive.info/contacts/";

https statt http wird es

arbeite ich den Code selbst heruntergeladen und überprüft sie. Tatsächlich wird der Fehler jederzeit angezeigt, auch wenn die Internetverbindung fehlt. Diese Demo ist sehr einfach und hat Ausnahmen nicht sehr gut gemacht. Wenn also die Anfrage fehlschlägt, wird eine leere Zeichenfolge von sh.makeServiceCall (url) zurückgegeben. welches Android nicht in ein JSON konvertieren kann.

Stellen Sie also sicher, dass die Internetverbindung auf dem Gerät in Ordnung ist, andernfalls ändern Sie die Funktion sh.makeServiceCall (url).

Verwandte Themen