私は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();
}
}
こと。
どのようにString( "json_data")を受け取っていますか?私はそれのコードを見ることができません – Shuddh
編集された先生。私は受信コード – orange
を掲載しました。 Intent intent = getIntent(); 文字列s = intent.getStringExtra( "json_data"、0); – Shuddh