2016-04-05 16 views
-3

入手方法org.json.JSONExceptionjava.lang.Stringの値の結果をJSONObjectに変換できません。コードが間違っていますか?ライブストリーミング..completeコードから値を以下に示す取得.. JSONはorg.json.JSONException:java.lang.String型の値の結果をJSONObjectに変換できません

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" 
    }, 

コードを与え、あなたのlogcatを読み込むのと同じように

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

JSONとlogcatメッセージ – Exaqt

+0

を投稿してくださいI更新JSONとlogcat..Trying今すぐ正しい形式で投稿してください。 – RD87

+0

Thanks Praful Bhatnagar .. – RD87

答えて

0

org.json.JSONException: Value result of type java.lang.String cannot be converted to JSONObject

最初にデータを解析せずに取得した応答を記録します。次に、どのデータ型を扱っているかを知ることができます。例:Strings, Integers, Floats,など

ここに回答を投稿してください。

EDIT: この行は、ここでjsonObj = new JSONObject("result"); これを行う代わりに、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

あなたが言ったように変更されましたが、同じ例外が発生しました。応答と更新をログに記録します – RD87

+0

JSON全体が囲まれていますか? {race {..} ..}や[race {..} ..]のように、{race {}}として囲まれた –

+0

.. '{'はコピー貼り付け中に逃しました。 – RD87

関連する問題