【问题标题】:json part in Mainactivity is not working on 4.2(api 17) [duplicate]Mainactivity中的json部分不适用于4.2(api 17)[重复]
【发布时间】:2013-10-27 19:53:20
【问题描述】:

我在 Mainactivity 中带有 json 部分的 android 应用程序无法在 4.2(api 17) 上运行
(不幸的是应用程序已停止)对话框出现。 但在 2.2(api 8),2.3.5(api 10) 中正常工作。 在不同设备上使用相同代码的“android 版本”是否有任何问题。 ?

private static String url = "http://api.androidhive.info/contacts/";

    // JSON Node names
    private static final String TAG_CONTACTS = "contacts";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_EMAIL = "email";

    private static final String TAG_PHONE = "phone";
    private static final String TAG_PHONE_MOBILE = "mobile";


    // contacts JSONArray
    JSONArray contacts = null;


Button login_btn,other;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);// Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting Array of Contacts
        contacts = json.getJSONArray(TAG_CONTACTS);

        // looping through All Contacts
        for(int i = 0; i < contacts.length(); i++){
            JSONObject c = contacts.getJSONObject(i);

            // Storing each json item in variable
            String id = c.getString(TAG_ID);
            String name = c.getString(TAG_NAME);
            String email = c.getString(TAG_EMAIL);


            // Phone number is agin JSON Object
            JSONObject phone = c.getJSONObject(TAG_PHONE);
            String mobile = phone.getString(TAG_PHONE_MOBILE);


            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put(TAG_ID, id);
            map.put(TAG_NAME, name);
            map.put(TAG_EMAIL, email);
            map.put(TAG_PHONE_MOBILE, mobile);

            // adding HashList to ArrayList
            contactList.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }


    /**
     * Updating parsed JSON data into ListView
     * */
    ListAdapter adapter = new SimpleAdapter(this, contactList,
            R.layout.list_item,
            new String[] { TAG_NAME, TAG_EMAIL, TAG_PHONE_MOBILE }, new int[] {
                    R.id.name, R.id.email, R.id.mobile });

    setListAdapter(adapter);

    // selecting single ListView item
    ListView lv = getListView();

    // Launching new screen on Selecting Single ListItem
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String cost = ((TextView) view.findViewById(R.id.email)).getText().toString();
            String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
            in.putExtra(TAG_NAME, name);
            in.putExtra(TAG_EMAIL, cost);
            in.putExtra(TAG_PHONE_MOBILE, description);
            startActivity(in);

        }
    });

【问题讨论】:

  • 你在哪里运行 Json 部分以及从哪里加载它?
  • 另外,贴出你的代码
  • 私有静态字符串 url = "api.androidhive.info/contacts";从这里我加载 json 部分

标签: android json


【解决方案1】:

最近的 android 版本不允许您在主线程中执行网络操作等活动,因为它们可能导致 ANR。因此,下载 json 的推荐和正确方法是通过 AsyncTask。

【讨论】:

    【解决方案2】:

    androidhive 懒得再更新他们的教程了。如果你看

    public JSONObject getJSONFromUrl,

    然后您会看到他们确实按照建议在主线程上作为电池进行网络连接。他们甚至没有捕捉到您的代码无疑会抛出的NetworkOnMainThreadException。您需要将网络移出 UI 线程。

    【讨论】:

    猜你喜欢
    • 2013-04-27
    • 2017-07-24
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多