1

所以我想从一个看起来像这样的网站解析 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这里是我尝试使用示例输入访问的站点:

http://www.railpnrapi.com/6520652825

4

1 回答 1

0

您正在尝试JSONObject从传递给构造函数的 URL 字符串创建,这将不起作用。

您需要将 URL 的内容获取到 a String,然后将该字符串传递给JSONObject构造函数。

有关详细信息,请参阅此 SO 答案。关于如何做到这一点。

于 2013-07-01T20:44:28.707 回答