org.json.JSONObject cannot be converted to JSONArray in android

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

When I try in my localhost it work find. this is the JSON that provide by my localhost.

Y it error in this url http://api.androidhive.info/contacts/

         [{      "id": "1",      "first_name": "man",      "last_name": "woman",      "username": "man",      "password": "4f70432e6369 70de9929bcc6f1b72412",      "email": "man@gmail.com",      "url": "http:\/\/localhost\/adchara1\/"  }, {      "id": "6",      "first_name": "first",      "last_name": "Last  Name",      "username": "user",      "password": "1a1dc91c907325c69271ddf0c944bc72",      "email": "0",      "url": "ht tp:\/\/api.androidhive.info\/contacts\/"  }, {      "id": "7",      "first_name": "1",      "last_name": "2",      "username": "us45",      "password": "33d8f54e33896a5722 7b18642979e558",      "email": "first@gmail.com",      "url": "http:\/\/ugirusgiarto.wordpress.com\/2011\ /10\/27\/json-php-mysql-with-asynctask-progressdialog\/"  }, {      "id": "9",      "first_name": "First Name",      "last_name": "Last  Name",      "username": "Username",      "password": "dc647eb65e6711e155375218212b3964",      "email": "woman@gm ail.com",      "url": "http:\/\/mobile.cs.fsu.edu\/parse-json-objects-in-asynctask\/"  }] 

x

MainActivity

public class MainActivity extends Activity {     TextView text_1, text_2;      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          new task().execute();     }      class task extends AsyncTask {         private ProgressDialog progressDialog = new ProgressDialog(                 MainActivity.this);         InputStream is = null;         String result = "";          protected void onPreExecute() {             progressDialog.setMessage("Download data...");             progressDialog.show();             progressDialog.setOnCancelListener(new OnCancelListener() {                 public void onCancel(DialogInterface arg0) {                     task.this.cancel(true);                 }             });         }          @Override         protected Void doInBackground(String... params) { //          String url_select = "http://192.168.10.111/adchara1/";              String url_select = "http://api.androidhive.info/contacts/";              HttpClient httpClient = new DefaultHttpClient();             HttpPost httpPost = new HttpPost(url_select);              ArrayList param = new ArrayList();              try {                 httpPost.setEntity(new UrlEncodedFormEntity(param));                  HttpResponse httpResponse = httpClient.execute(httpPost);                 HttpEntity httpEntity = httpResponse.getEntity();                  // read content                 is = httpEntity.getContent();              } catch (Exception e) {                  Log.e("log_tag", "Error in http connection " + e.toString());             }             try {                 BufferedReader br = new BufferedReader(                         new InputStreamReader(is));                 StringBuilder sb = new StringBuilder();                 String line = "";                 while ((line = br.readLine()) != null) {                     sb.append(line + "\n");                 }                 is.close();                 result = sb.toString();              } catch (Exception e) {                 // TODO: handle exception                 Log.e("log_tag", "Error converting result " + e.toString());             }              return null;          }          protected void onPostExecute(Void v) {              // ambil data dari Json database             try {                 JSONArray Jarray = new JSONArray(result);                 for (int i = 0; i 

ERROR

Error parsing data org.json.JSONException: Value {     "contacts": [{         "id": "c200",         "gender": "male",         "phone": {             "office": "00 000000",             "home": "00 000000",             "mobile": "+91 0000000000"         },         "address": "xx-xx-xxxx,x - street, x - country",         "email": "ravi@gmail.com",         "name": "Ravi Tamada"     }, {         "id": "c201",         "gender": "male",         "phone": {             "office": "00 000000",             "home": "00 000000",             "mobile": "+91 0000000000"         },         "address": "xx-xx-xxxx,x - street, x - country",         "email": "johnny_depp@gmail.com",         "name": "Johnny Depp"     }, {         "id": "c202",         "gender": "male",         "phone": {             "office": "00 000000",             "home": "00 000000",             "mobile": "+91 0000000000"         },         "address": "xx-xx-xxxx,x - street, x - country",         "email": "leonardo_dicaprio@gmail.com",         "name": "Leonardo Dicaprio"     }, {         "id": "c203",         "gender": "male",         "phone": {             "office": "00 000000",             "home": "00 000000",             "mobile": "+91 0000000000"         },         "address": "xx-xx-xxxx,x - street, x - country",         "email": "john_wayne@gmail.com",         "name": "John Wayne"     }, {         "id": "c204",         "gender": "female",         "phone": {             "office": "00 000000",             "home": "00 000000",             "mobile": "+91 0000000000"         },         "address": "xx-xx-xxxx,x - street, x - country",         "email": "angelina_jolie@gmail.com",         "name": "Angelina Jolie"     }, {         "id": "c205",         "gender": "female",         "phone": {             "office": "00 000000",             "home": "00 000000",             "mobile": "+91 0000000000"         },         "address": "xx-xx-xxxx,x - street, x - country",         "email": "dido@gmail.com",         "name": "Dido"     }, {         "id": "c206",         "gender": "female",         "phone": {             "office": "00 000000",             "home": "00 000000",             "mobile": "+91 0000000000"         },         "address": "xx-xx-xxxx,x - street, x - country",         "email": "adele@gmail.com",         "name": "Adele"     }, {         "id": "c207",         "gender": "male",         "phone": {             "office": "00 000000",             "home": "00 000000",             "mobile": "+91 0000000000"         },         "address": "xx-xx-xxxx,x - street, x - country",         "email": "hugh_jackman@gmail.com",         "name": "Hugh Jackman"     }, {         "id": "c208",         "gender": "male",         "phone": {             "office": "00 000000",             "home": "00 000000",             "mobile": "+91 0000000000"         },         "address": "xx-xx-xxxx,x - street, x - country",         "email": "will_smith@gmail.com",         "name": "Will Smith"     }, {         "id": "c209",         "gender": "male",         "phone": {             "office": "00 000000",             "home": "00 000000",             "mobile": "+91 0000000000"         },         "address": "xx-xx-xxxx,x - street, x - country",         "email": "clint_eastwood@gmail.com",         "name": "Clint Eastwood"     }, {         "id": "c2010",         "gender": "male",         "phone": {             "office": "00 000000",             "home": "00 000000",             "mobile": "+91 0000000000"         },         "address": "xx-xx-xxxx,x - street, x - country",         "email": "barack_obama@gmail.com",         "name": "Barack Obama"     }, {         "id": "c2011",         "gender": "female",         "phone": {             "office": "00 000000",             "home": "00 000000",             "mobile": "+91 0000000000"         },         "address": "xx-xx-xxxx,x - street, x - country",         "email": "kate_winslet@gmail.com",         "name": "Kate Winslet"     }, {         "id": "c2012",         "gender": "male",         "phone": {             "office": "00 000000",             "home": "00 000000",             "mobile": "+91 0000000000"         },         "address": "xx-xx-xxxx,x - street, x - country",         "email": "eminem@gmail.com",         "name": "Eminem"     }] } of type org.json.JSONObject cannot be converted to JSONArray 

回答1:

You could try this:

JSONObject object = new JSONObject(result); JSONArray Jarray  = object.getJSONArray("contacts");  for (int i = 0; i 


回答2:

The url (http://api.androidhive.info/contacts/) returns a JSON object and not a JSON array...

Here:

JSONArray Jarray = new JSONArray(result);     for (int i = 0; i 

You are assuming the result is a json array. If you do it like the following it will work:

JSONObject Jasonobject = new JSONObject(result); JSONArray Jarray = Jasonobject.getJSONArray("contacts");  for (int i = 0; i 


回答3:

try using this

JSONObject Jarray = new JSONObject(result); 

instead of

JSONArray Jarray = new JSONArray(result); 

and next

JSONArray Jarray = Jasonobject.getJSONArray("contacts"); 

and the for loop continues

Download image from URL

Pass the URL to this particular method and you can download the image to the gridview

private void downloadImage(String urlStr) {     final String url = urlStr;             InputStream in = null;             Message msg = Message.obtain();             msg.what = 1;             try {                 in = openHttpConnection(url);                 bitmap = BitmapFactory.decodeStream(in);                 Bundle b = new Bundle();                 b.putParcelable("bitmap", bitmap);                 msg.setData(b);                 in.close();              } catch (Exception e1) {                 e1.printStackTrace();             }            }  private InputStream openHttpConnection(String urlStr) {     InputStream in = null;     int resCode = -1;      try {         URL url = new URL(urlStr);         URLConnection urlConn = url.openConnection();          if (!(urlConn instanceof HttpURLConnection)) {             throw new IOException ("URL is not an Http URL");         }          HttpURLConnection httpConn = (HttpURLConnection)urlConn;         httpConn.setAllowUserInteraction(false);         httpConn.setInstanceFollowRedirects(true);         httpConn.setRequestMethod("GET");         httpConn.connect();          resCode = httpConn.getResponseCode();                         if (resCode == HttpURLConnection.HTTP_OK) {             in = httpConn.getInputStream();                                         }             } catch (MalformedURLException e) {         e.printStackTrace();     } catch (IOException e) {         e.printStackTrace();     }     return in; } 


回答4:

try the following to convert :

         JSONObject js= new JSONObject(yourjsonobjectvalue);          JSONArray jA = js.getJSONArray("String value"); 


回答5:

The error indicates that your JSON is a JSON Object and not an JSON Array:

change:

JSONArray Jarray = new JSONArray(result) 

to

JSONObject someObject = new JSONObject(result); JSONArray Jarray = Jarray.getJSONArray("contacts"); 


回答6:



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!