2017-01-11 5 views
0

私はこれらから来るデータを取得しようとしています上のXMLやJSONを取得する*リンク*
http://21.26.54.26/swapi/api/data/json
http://21.26.54.26/swapi/api/data/xmlコードは、Java

私は多くの方法を試みたが、最終的に、それは常に私にエラー時にそれを与えますこのラインを通過JSONObject jsonObj = new JSONObject(json);

OkHttpClient client = new OkHttpClient(); 
      Request request = new Request.Builder() 
        .url("http://********/swapi/api/data/xml") 
        .build(); 


      Response responses = null; 

      try { 
       responses = client.newCall(request).execute(); 

       json = responses.body().string(); 

       System.out.println("JSON: " + json); 

       try { 


        JSONObject jsonObj = new JSONObject(json); 


        // JSONObject obj = new JSONObject(jsonObj); 

        // Getting JSON Array node 
        JSONArray contacts = jsonObj.getJSONArray("Carro"); 
        for (int i = 0; i < contacts.length(); i++) { 
         JSONObject c = contacts.getJSONObject(i); 
         String id = c.getString("idintelisis"); 
         System.out.println("TESTEEEEEEEEEEEEEEEEEEEEEEE: " + id); 


        } 
       } catch (JSONException ex) { 

        System.out.println("Errrrrrrrrrrrrrrrrrrrrrrrrorrrrrrrrrrrrrrrrrrr: " + ex); 
       } 


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

これは誤りである**長い**それはその行に分割したときには、スロー

01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: org.json.JSONException: Value [{"idintelisis":101,"observaciones":"Nitin","fecha":null,"carro":null,"cargapkt":"sd5"},{"idintelisis":101,"observaciones":"Nitin","fecha":null,"carro":null,"cargapkt":"sd5"}] of type java.lang.String cannot be converted to JSONObject 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at org.json.JSON.typeMismatch(JSON.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at org.json.JSONObject.(JSONObject.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at org.json.JSONObject.(JSONObject.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at pa.com.tropigas.pedidostf.CarrosActivity$POST.doInBackground(CarrosActivity.java:114) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at pa.com.tropigas.pedidostf.CarrosActivity$POST.doInBackground(CarrosActivity.java:65) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at java.lang.Thread.run(Thread.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf I/System.out: Error: org.json.JSONException: Value [{"idintelisis":101,"observaciones":"Nitin","fecha":null,"carro":null,"cargapkt":"sd5"},{"idintelisis":101,"observaciones":"Nitin","fecha":null,"carro":null,"cargapkt":"sd5"}] of type java.lang.String cannot be converted to JSONObject

私は、** Json **と** log **の** Json **の結果をチェックするために** Postman **を使用しており、すべて** Json **が正しいと示されています何を取得していることJsonArrayないJsonObject

この

JsonArray arr = new JsonArray(obtained_result); 

してから試してくださいを試しているので、構造の私は* JSON *オブジェクト*

+0

ルック。データは純粋な 'JSON'ではありません。 ''タグで囲まれています。そのため、 'JSON'は' XML'で囲まれています。 'JSON'を手に入れたら、@Jayanthsの答えを使うことができます。 –

答えて

1

そのに変換しようとすると壊れる何を理解していませんJsonObjectをから取得するgetJSONObject(index)方法使っ

例:再びデータで

JsonArray arr = new JsonArray(obtained_result); 
    for(int i = 0; i< arr.length(); i++){ 
    JsonObject ob = arr.getJSONObject(i); 
}