0

我跟着这个教程

这是我的最终代码MainActivity.java

@SuppressLint({ "NewApi", "NewApi" })
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Permission StrictMode
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    // listView1
    final ListView lisView1 = (ListView)findViewById(R.id.listView1);   

    /** JSON return
     *  [{"MemberID":"1","Name":"Weerachai","Tel":"0819876107"},
     * {"MemberID":"2","Name":"Win","Tel":"021978032"},
     * {"MemberID":"3","Name":"Eak","Tel":"0876543210"}]
     */

    String url = "http://10.0.2.2/Json/getJSON2.php";

    try {

        JSONArray data = new JSONArray(getJSONUrl(url));

        final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;

        for(int i = 0; i < data.length(); i++){
            JSONObject c = data.getJSONObject(i);

            map = new HashMap<String, String>();
            map.put("MemberID", c.getString("MemberID"));
            map.put("Name", c.getString("Name"));
            map.put("Tel", c.getString("Tel"));
            MyArrList.add(map);

        }


        SimpleAdapter sAdap;
        sAdap = new SimpleAdapter(MainActivity.this, MyArrList, R.layout.activity_column,
                new String[] {"MemberID", "Name", "Tel"}, new int[] {R.id.ColMemberID, R.id.ColName, R.id.ColTel});      
        lisView1.setAdapter(sAdap);

        final AlertDialog.Builder viewDetail = new AlertDialog.Builder(this);
        // OnClick Item
        lisView1.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> myAdapter, View myView,
                    int position, long mylng) {

                String sMemberID = MyArrList.get(position).get("MemberID")
                        .toString();
                String sName = MyArrList.get(position).get("Name")
                        .toString();
                String sTel = MyArrList.get(position).get("Tel")
                        .toString();

                //String sMemberID = ((TextView) myView.findViewById(R.id.ColMemberID)).getText().toString();
                // String sName = ((TextView) myView.findViewById(R.id.ColName)).getText().toString();
                // String sTel = ((TextView) myView.findViewById(R.id.ColTel)).getText().toString();

                viewDetail.setIcon(android.R.drawable.btn_star_big_on);
                viewDetail.setTitle("Member Detail");
                viewDetail.setMessage("MemberID : " + sMemberID + "\n"
                        + "Name : " + sName + "\n" + "Tel : " + sTel);
                viewDetail.setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                dialog.dismiss();
                            }
                        });
                viewDetail.show();

            }
        });


    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}


public String getJSONUrl(String url) {
    StringBuilder str = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) { // Download OK
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                str.append(line);
            }
        } else {
            Log.e("Log", "Failed to download result..");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return str.toString();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

然后当我运行程序进行测试时,我的 logcat 发现了这个警告:

模拟器上的结果是列表视图的空白屏幕。

我仍然不知道如何解决它,有人有想法吗?

4

2 回答 2

2

我认为响应 JSON 格式错误。这就是你得到异常的原因。在将其转换为 JSON 之前,请检查响应字符串。

于 2012-10-17T07:59:11.910 回答
0

您可以在调试器中运行它并检查数据的值,但很可能这就是发生错误的地方,所以也许在这一行:return str.toString(); 在 getJSONUrl 方法中,您可以检查 str 的值。

您的网络服务器发现的任何错误都将作为错误消息包含在内,这可能就是您的代码令人窒息的原因。

于 2012-08-29T10:50:30.583 回答