そこで、次のようなサイトから Json ファイルを解析したいと考えました。
{ "tnum": "12805", "tname": "JANMABHOOMI EXP", "tdate": "25-2-2013", "from": "GNT", "to": "SC", "class": "CC", "charted": true, "numofpax": 2, "pax": [{ "coach": "D2", "berth": "23", "quota": "GN", "status": "CNF" }, { "コーチ": "S2", "バース": "43", "クォータ": "GN", "ステータス": "CNF" } ] }
私が書いた不完全なコードは次のとおりです。
EditText num = (EditText)findViewById(R.id.pnr_num);
String numPNR= num.getText().toString();
url = "http://www.railpnrapi.com/";
url = url.concat(numPNR);
TextView t = (TextView)findViewById(R.id.textView1);
t.setText(url);
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(url);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// grabbing the menu object
try {
String id = jsonObj.getString("tname");
TextView t1 = (TextView)findViewById(R.id.textView1);
t1.setText(id);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ただし、アプリがクラッシュするたびに。ネットワークアクセス許可も追加しました!
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
どこで間違いを犯したのですか?ありがとう。PS サンプル入力でアクセスしようとしているサイトは次のとおりです。