Android(未处理的异常 : org. json.JSONException)

标签 android json exception unhandled

伙计们。无法将 JSON 字符串/对象输出到 TextView。 几天前开始在 Android Studio 中编程,所以......需要你们的帮助,伙计们。 我有 mainActivity、AllProductsActivity 和 EditProductActivity。所有产品 Activity 从 mysql 数据库获取 JSON 数组并显示在 ListView 中。单击 List Item 后,它会转到 EditProductActivity。这很好用。但是我不明白我需要用这个错误做什么......

class GetProductDetails extends AsyncTask<String, String, JSONObject> {


    //Show progress dialog

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(EditProductActivity.this);
        pDialog.setMessage("Loading product details. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }


    //Getting detail info in background


    protected JSONObject doInBackground(String... args) {

        JSONObject product = null;
        // check status success tag
        int success;

        try {
            // parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("pid", pid));

            // getting product from HTTP
            JSONObject json = jsonParser.makeHttpRequest(url_product_details, "GET", params);

            Log.d("Single Product Details", json.toString());

            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                // Getting detail info successful
                JSONArray productObj = json.getJSONArray(TAG_PRODUCT);

                // first object from JSON Array
                product = productObj.getJSONObject(0);

            } else {
                // product with that pid not found
                Toast.makeText(getApplicationContext(), "pid not found", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getApplicationContext(), AllProductsActivity.class);
                startActivity(intent);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return product;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * *
     */
    protected void onPostExecute(JSONObject product) {
        if (product != null) {
            setContentView(R.layout.activity_edit_product);

            // product with this pid found
            // Text View
            txtName = (TextView) findViewById(R.id.showName);
            txtPrice = (TextView) findViewById(R.id.showPrice);
            txtDesc = (TextView) findViewById(R.id.showDescription);

            // display product data in EditText

           txtName.setText(product.getString(TAG_NAME));
          txtPrice.setText(product.getString(TAG_PRICE));
           txtDesc.setText(product.getString(TAG_DESCRIPTION));

        }
        // close progress dialog
        pDialog.dismiss();
    }
}

错误:

txtName.setText(product.getString(TAG_NAME));
txtPrice.setText(product.getString(TAG_PRICE));
txtDesc.setText(product.getString(TAG_DESCRIPTION));

未处理的异常:org.json.JSONException

最佳答案

添加一个 try/catch

try {
            txtName.setText(product.getString(TAG_NAME));
            txtPrice.setText(product.getString(TAG_PRICE));
            txtDesc.setText(product.getString(TAG_DESCRIPTION));
        } catch (JSONException e) {
            // Do something with the exception
        }

关于Android(未处理的异常 : org. json.JSONException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30289189/

相关文章:

javascript - 将 JSON 数据转换为 JavaScript 二维数组

java - 多次抛出的异常是检查的还是运行时的?

android - 运行 “cordova build android” - AAPT : error: resource android:attr/fontVariationSettings not found.\n android:attr/ttcIndex 未找到

java - 在RestoreInstanceState上获取所有View对象

javascript - 从 JS "string"更新值属性

javascript - 获取 javascript 中最后一次出现的 json 元素的值

android - 通过 logcat 在 Android 上调试 WebView (Ionic) 应用程序

java - Android ViewPager fragment - 没有滑动和预加载?

c++ - 抛出 std::out_of_range 异常 C++

java - 避免在 Java 中使用相同的 try catch block