2016-04-05 18 views
-3

Erste org.json.JSONException: Wert Ergebnis vom Typ java.lang.String kann nicht auf JSONObject ..wo wäre umgewandelt werden, um Code falsch gelaufen Dies ist ein Programm, erhalten Werte von einem Code-Live-Streaming ..complete unten angegeben ist .. JSONorg.json.JSONException: Wert Ergebnis vom Typ java.lang.String kann nicht in JSONObject umgewandelt wird

gegeben
race: { 
    id: "44708", 
    track_id: "1", 
    track: "International", 
    starts_at: "2016-05-04 06:16:00", 
    finish_time: "1970-01-01 01:00:00", 
    heat_type_id: "123", 
    heat_status_id: "1", 
    speed_level_id: "5", 
    speed_level: "Nat Cadet & Junior", 
    win_by: "position", 
    race_by: "minutes", 
    duration: 10, 
    race_name: "Inkart National Heat", 
    race_time_in_seconds: 276.336 
}, 
scoreboard: [ 
    { 
    position: "1", 
    nickname: "Emily Linscott", 
    average_lap_time: "90.053", 
    fastest_lap_time: "60.490", 
    last_lap_time: "60.490", 
    rpm: "1225", 
    first_name: "Emily", 
    last_name: "Linscott", 
    is_first_time: "0", 
    total_races: "6", 
    racer_id: "1157509", 
    lap_num: "3", 
    kart_num: "63", 
    gap: ".000", 
    ambtime: "31728819591" 
    }, 

-Code

protected JSONObject doInBackground(String... urls) { 
     JSONObject jsonObj = null; 
     Log.d("TAG","inside doInBackground.."); 
     try { 
      URL url = new URL("http://daytonamk.clubspeedtiming.com/api/index.php/races/scoreboard.json?track_id=1&key=cs-dev" + kartNumber); 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
      urlConnection.setRequestMethod("GET"); 


      BufferedInputStream inputStream = new BufferedInputStream(urlConnection.getInputStream()); 
      String result = convertInputStreamToString(inputStream); 
      parseResult(result); 

      Log.e("Message", "This is a message"); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return jsonObj; 
    } 

    private String convertInputStreamToString(InputStream inputStream) throws IOException { 
     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
     StringBuilder builder = new StringBuilder(); 
     String line; 
     String result = ""; 

     { 
      while ((line = bufferedReader.readLine()) != null) { 
       builder.append(line); 
      } 




      return result; 

     } 

    } 


    private JSONObject parseResult(String result) { 
     JSONObject jsonObj = null; 
     if (result == null) { 
      Toast.makeText(Main_Activity.this, "Race not currently running", Toast.LENGTH_LONG).show(); 
     } else { 
      try { 
       int number = 0; 


       jsonObj = new JSONObject("result"); 

       JSONObject race = jsonObj.getJSONObject("race"); 
       int durationInMins = race.getInt("duration"); 
       String win_by = race.getString("win_by"); 

       JSONArray scoreboard = jsonObj.getJSONArray("scoreboard"); 
       Log.d("TAG", "Json value :" + scoreboard); 
       for (int i = 0; i < scoreboard.length(); i++) { 
        JSONObject data = scoreboard.getJSONObject(i); 


        number = data.getInt("kart_num"); 


        while (kartNumber == number) { 
         int gridPosition = data.getInt("position"); 
         int gap = data.getInt("gap"); 
         int lastLapTime = data.getInt("last_lap_time"); 
         int bestLapTime = data.getInt("fastest_lap_time"); 


         int raceInTime = race.getInt("race_time_in_seconds"); 

         duration = durationInMins * 60 - raceInTime; 


         if (!(gridPosition == 1)) { 
          JSONObject dataPreviousGap = scoreboard.getJSONObject(i - 1); 
          int gapPrev = dataPreviousGap.getInt("gap"); 
          gapUp = gap - gapPrev; 
         } 
         if (!(i == scoreboard.length() - 1)) { 
          JSONObject dataNext = scoreboard.getJSONObject(i + 1); 
          int gapNext = dataNext.getInt("gap"); 
          gapDown = gapNext - gap; 
         } 
        } 
       } 


      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
     return jsonObj; 
    } 


    protected void onPostExecute(JSONObject jsonObj) { 

     Intent myIntent = new Intent(Main_Activity.this, Display_Activity.class); 

     try { 
      JSONObject jsonObje = new JSONObject("result"); 

     JSONArray scoreboard = jsonObje.getJSONArray("scoreboard"); 
     for (int i = 0; i < scoreboard.length(); i++) { 
     JSONObject data = scoreboard.getJSONObject(i); 
     myIntent.putExtra("GridPosition", data.getInt("position")); 
     myIntent.putExtra("LastLapTime", data.getInt("last_lap_time")); 
     myIntent.putExtra("MinutesToGo", duration); 
     myIntent.putExtra("BestLapTime",data.getInt("fastest_lap_time")); 
     myIntent.putExtra("GapUp", gapUp); 
     myIntent.putExtra("GapDown", gapDown); 

     } 
+0

Bitte senden Sie das json und logcat Nachricht – Exaqt

+0

I aktualisiert json und logcat..Trying um es im richtigen Format jetzt zu posten .. – RD87

+0

Danke Praful Bhatnagar .. – RD87

Antwort

0

Genau wie Ihr logcat liest org.json.JSONException: Value result of type java.lang.String cannot be converted to JSONObject

Protokollieren Sie die Antwort, die Sie erhalten, ohne zuerst die Daten zu analysieren. Dann können Sie herausfinden, um welche Datentypen es sich handelt, zB: Strings, Integers, Floats, etc.

Geben Sie die Antwort hier für weitere Hilfe ein.

EDIT: Diese Zeile hier jsonObj = new JSONObject("result"); dies tun, anstatt jsonObj = new JSONObject(result);

EDIT:

jsonObj = new JSONObject(result); 
 
JSONObject obj = jsonObj.getJSONObject("race"); 
 
String id = obj.getString("id"); 
 
//do the same for all objects inside race

+0

Geändert, wie Sie sagten..aber die gleiche Ausnahme erhalten..wird die Antwort protokollieren und aktualisieren – RD87

+0

Ist der gesamte JSON eingeschlossen? Wie in {race {..} ..} oder [race {..} ..] –

+0

eingeschlossen als {race {}} .. '{' wurde beim Kopieren-Einfügen verpasst. – RD87

Verwandte Themen