2016-10-27 20 views
0

私はJSONを初めて使用しています。私は助けが必要です。私のアンドロイドスタジオは、私のjsonobjectがNULLであることを私に伝え続けます。私はjsonarrayを解析してリストビューに表示することができます。しかし、私がそれを表示したページをクリックすると、私のアプリケーションがクラッシュします。エラー指示は、{ jsonObject =新しいJSONObject(json_string)試すここでandroid jsonObjectヌルポインタ例外

パーサー

class BgTask extends AsyncTask<Void, Void, String> { 
    String json_url; 

    @Override 
    protected void onPreExecute() { 
     json_url = "http://10.0.2.2/result/hehe.php"; 
    } 

    @Override 
    protected String doInBackground(Void... params) { 
     try { 
      URL url = new URL(json_url); 
      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
      InputStream inputstream = httpURLConnection.getInputStream(); 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputstream)); 
      StringBuilder stringBuilder = new StringBuilder(); 
      while ((JSON_STRING = bufferedReader.readLine()) != null) { 
       stringBuilder.append(JSON_STRING + "\n"); 
      } 

      bufferedReader.close(); 
      inputstream.close(); 
      httpURLConnection.disconnect(); 
      return stringBuilder.toString().trim(); 

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

     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
     super.onProgressUpdate(values); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     json_string = result; 
    } 
} 

public void publishGame(View view) 
{ 
    if(json_string == null) 
    { 
     Toast.makeText(getApplicationContext(), "Get Data First",Toast.LENGTH_SHORT).show(); 
    } 
    else 
    { 
     Intent intent = new Intent(this, Games.class); 
     intent.putExtra("json_data", json_string); 
     startActivity(intent); 
    } 
} 

} コードポスト

Bundle intent=getIntent().getExtras(); 
    if(intent !=null) { 
     json_string = intent.getString("json_data"); 

     json_string = getIntent().getExtras().getString("json_data"); 
    } 

try { 
      jsonObject = new JSONObject(json_string); 
      jsonArray = jsonObject.getJSONArray("server_response"); 
      int count = 0; 
      String team1, score1, team2, score2, Type; 

      while (count < jsonArray.length()) { 
       JSONObject JO = jsonArray.getJSONObject(count); 
       team1 = JO.getString("team1"); 
       score1 = JO.getString("score1"); 
       team2 = JO.getString("team2"); 
       score2 = JO.getString("score2"); 
       Type = JO.getString("Type"); 
       Downloader downloader = new Downloader(team1, score1, team2, score2, Type); 
       gamesAdapter.add(downloader); 

       count++; 
      } 


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

} 

こと。

+0

どのようにString( "json_data")を受け取っていますか?私はそれのコードを見ることができません – Shuddh

+0

編集された先生。私は受信コード – orange

+0

を掲載しました。 Intent intent = getIntent(); 文字列s = intent.getStringExtra( "json_data"、0); – Shuddh

答えて

1

"json_data"JSONObjectを有効なjsonでないと解析しようとしているためにエラーが発生しました。おそらく、をJSONObject constructorに渡すのではなく、variableのサーバー応答がStringタイプの場合は、を渡す必要がある場合、json_dataという名前のサーバー応答があります。

+0

私はこのエラーを申し訳ありません。それはjson_stringではなく、 "json_data" – orange

関連する問題