2012-10-04 6 views
8

में JSONArray में परिवर्तित नहीं किया जा सकता है जब मैं अपने लोकहोस्ट में कोशिश करता हूं तो यह काम करता है। यह जेएसओएन है जो मेरे लोकहोस्ट द्वारा प्रदान किया जाता है।org.json.JSONObject को एंड्रॉइड

इस यूआरएल में

Y यह त्रुटि http://api.androidhive.info/contacts/

  [{ 
    "id": "1", 
    "first_name": "man", 
    "last_name": "woman", 
    "username": "man", 
    "password": "4f70432e6369 
70de9929bcc6f1b72412", 
    "email": "[email protected]", 
    "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": "[email protected]", 
    "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": "[email protected] 
ail.com", 
    "url": "http:\/\/mobile.cs.fsu.edu\/parse-json-objects-in-asynctask\/" 
}] 

एक्स

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<String, String, Void> { 
     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<NameValuePair> param = new ArrayList<NameValuePair>(); 

      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 < Jarray.length(); i++) { 
        JSONObject Jasonobject = null; 
        text_1 = (TextView) findViewById(R.id.txt1); 
        Jasonobject = Jarray.getJSONObject(i); 

        // get an output on the screen 
        String id = Jasonobject.getString("id"); 
        String name = Jasonobject.getString("name"); 
        String email = Jasonobject.getString("email"); 
        String address = Jasonobject.getString("address"); 
        String gender = Jasonobject.getString("gender"); 
        text_1.append("\n" + id + "\t\t" + name + "\t\t\t" 
          + email + "\t\t\t\t" + address + "\t\t\t\t" + gender 
          + "\t\t\t\t" + "\n"); 

       } 
       this.progressDialog.dismiss(); 

      } catch (Exception e) { 
       // TODO: handle exception 
       Log.e("log_tag", "Error parsing data " + e.toString()); 
      } 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
} 

त्रुटि

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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "name": "Eminem" 
    }] 
} 
of type org.json.JSONObject cannot be converted to JSONArray 
+0

करने के लिए छवि डाउनलोड कर सकते हैं मैं सुझाव देता हूं कि आप जो कुछ जानते हैं उसके साथ प्रयास करना एक वैध JSONArray है यह सुनिश्चित करने के लिए कि आपका कोड काम करता है और फिर यह देखने के लिए जांच कर रहा है यह वास्तव में एक वैध JSONArray है। आप Google Chrome के "JSON Formatter" एक्सटेंशन जैसे टूल का उपयोग कर सकते हैं ताकि आप इंडेंट, स्वरूपित तरीके से JSON को पढ़ने में मदद कर सकें –

उत्तर

15

आप इस कोशिश कर सकते:

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

for (int i = 0; i < Jarray.length(); i++) 
{ 
    JSONObject Jasonobject = Jarray.getJSONObject(i); 
} 
+1

क्या आप इसे हल कर सकते हैं http://stackoverflow.com/questions/12739824/image-viewer-get-image-from- url-that-provide-image-path-as-json-string – kongkea

+1

@RajaReddy PolamReddy उत्कृष्ट उत्तर –

1

कन्वर्ट करने के लिए निम्न प्रयास करें:

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

त्रुटि इंगित करता है कि आपके JSON एक JSON Object, न कि किसी JSON Array है:

परिवर्तन:

JSONArray Jarray = new JSONArray(result) 

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

क्या आप इस http: // stackoverflow को हल कर सकते हैं।कॉम/प्रश्न/12739824/छवि-दर्शक-प्राप्त-छवि-से-url-that-provide-image-path-as-json-string – kongkea

4

के यूआरएल (http://api.androidhive.info/contacts/) एक JSON रिटर्न वस्तु और JSON सरणी नहीं ...

यहाँ:

JSONArray Jarray = new JSONArray(result); 
    for (int i = 0; i < Jarray.length(); i++) { 
     JSONObject Jasonobject = null; 

आप परिणाम मानते हैं एक json सरणी है। यह काम करेंगे आप इसे पसंद करते हैं, तो निम्न:

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

for (int i = 0; i < Jarray.length(); i++) { ...} 
+0

क्या आप इसे हल कर सकते हैं http://stackoverflow.com/questions/12739824/image- दर्शक-प्राप्त-छवि-से-url-that-provide-image-path-as-json-string – kongkea

3

उपयोग करने का प्रयास इस

JSONObject Jarray = new JSONObject(result); 
बजाय

JSONArray Jarray = new JSONArray(result); 

और अगले

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

और पाश के लिए URL

से जारी है

डाउनलोड छवि इस विशेष विधि के लिए URL दर्रा और आप 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; 
} 
+0

पिछले उत्तर के लिए बहुत बहुत धन्यवाद और यह उत्तर – kongkea

+0

आपका स्वागत है। हैप्पी कोडिंग –

+0

आपको बुरा नहीं लगता, अगर मैं चाहता हूं कि आप मेरी मदद करें – kongkea

0
 // getting JSON string from URL 
     JSONObject json = jParser.getJSONFromUrl(uri.toString()); 

     try { 
      //Obter objeto JSON 
      clientesJSONArray = json.optJSONArray(Clientes.TABELA); 

      //Se for 1 objeto não virá em JSONArray - Os objetos em JSON são separados 
      //por colchetes [] - No caso de um objeto, não será array e sim um simples 
      //objeto em JSON 
      if(clientesJSONArray==null){ 
       // means item is JSONObject instead of JSONArray 
       //json = obj.optJSONObject("offerRideResult"); 
       JSONObject obj = json.getJSONObject(Clientes.TABELA); 

       Clientes oCliente = new Clientes(); 
       oCliente.setCliente(obj.getString(Clientes.CLIENTE)); 
       oCliente.setCod_cliente(obj.getInt(Clientes.COD_CLIENTE)); 
       oCliente.setE_mail(obj.getString(Clientes.E_MAIL)); 
       oCliente.setUsuario(obj.getString(Clientes.USUARIO)); 
       oCliente.setUsuario(obj.getString(Clientes.SENHA)); 


       clientesList.add(oCliente); 

      }else{ 
       // Mais de um objeto JSON separado por colchetes [] - JSONArray ao invés JSONObject 
       for (int i = 0; i < clientesJSONArray.length(); i++) { 

        JSONObject obj = clientesJSONArray.getJSONObject(i); 
        Clientes oCliente = new Clientes(); 
        oCliente.setCliente(obj.getString(Clientes.CLIENTE)); 
        oCliente.setCod_cliente(obj.getInt(Clientes.COD_CLIENTE)); 
        oCliente.setE_mail(obj.getString(Clientes.E_MAIL)); 
        oCliente.setUsuario(obj.getString(Clientes.USUARIO)); 
        oCliente.setUsuario(obj.getString(Clientes.SENHA)); 


        clientesList.add(oCliente); 


       } 
      } 
संबंधित मुद्दे