JSON解析错误 - 无法将字符串转换为JSONArray

时间:2013-03-01 10:44:08

标签: java android arrays json

我在运行Android应用时遇到问题。这是一个简单的活动,旨在通过HTTP从远程数据库获取数据。

我在log cat中收到这两个错误:

  

在getJarrayFromString()上解析为json时出错;   org.json.JSONException:类型为java.lang.String的数据库不能   转换为JSONArray。

  

fillproductlist()出错:java.lang.NullPointerException

我不擅长Java / Android,这是我第一次在这里提出这个问题。我无法理解问题可能出现在我的应用程序的哪一层(我的Java代码,PHP或数据库代码)?

代码快照:

private void fillProductList() {

    if (utils.isInternet()
            && CustomHttpClient.isAddressOk(Utils.LINK_PRODUCTS)) {

        final int from = bid.size();
        final int nr = 5;
        long sqliteSize = db.size(mySqlDatabase.TABLE_BOOKS);
        if (from == 0) {
            // we add the post variables and values for the request
            postParameters.add(new BasicNameValuePair("from", String
                    .valueOf(0).toString()));
            postParameters.add(new BasicNameValuePair("nr", String.valueOf(
                    nr).toString()));
        } else {
            postParameters.add(new BasicNameValuePair("from", String
                    .valueOf(from).toString()));
            postParameters.add(new BasicNameValuePair("nr", String.valueOf(
                    nr).toString()));
        }
        postParameters.add(new BasicNameValuePair("title", titleSearch));
        postParameters.add(new BasicNameValuePair("code", codeSearch));
        postParameters.add(new BasicNameValuePair("module", moduleSearch));
        if (sortOrder != null)
            postParameters.add(new BasicNameValuePair("order", sortOrder));
        if (sortType != null)
            postParameters.add(new BasicNameValuePair("by", sortType));
        task = new RequestTask("books", postParameters);
        task.setOnTaskCompleted(new OnTaskCompletedListener() {
            public void onTaskStarted() {
                if (!rlLoading.isShown()) {
                    rlLoading.startAnimation(fadeIn());
                    rlLoading.setVisibility(View.VISIBLE);
                }
                IS_PRODUCTS_TASK = true;
            }

            public void onTaskCompleted(String result) {
                try {
                    if (result != "") {
                        // Enter the remote php link 
                        // we convert the response into json array
                        jarray = utils.getJarrayFromString(result);
                        int mysqlSize = (jarray.getJSONObject(0)
                                .getInt("numRows"));
                        Log.i(DEBUG, "From " + from + " to " + mysqlSize);
                        if (from <= mysqlSize) {
                            int rows;
                            // we check to see if there is 0
                            if (jarray.length() > 0) {

                                Log.i(DEBUG,
                                        "From "
                                                + from
                                                + " to "
                                                + Math.floor(mysqlSize / nr)
                                                * nr);
                                if (from + 5 <= Math.floor(mysqlSize / nr)
                                        * nr) {
                                    rows = jarray.length();
                                } else {
                                    rows = mysqlSize % nr + 1;
                                    Utils.IS_ENDED_PRODUCT_LIST = true;
                                }
                                ArrayList<String> list = new ArrayList<String>();
                                for (int i = 1; i < rows; i++) {
                                    JSONObject row = jarray
                                            .getJSONObject(i);
                                    bid.add(row.getInt("bid"));
                                    bTitle.add(row.getString("bTitle"));
                                    bCode.add(row.getString("bCode"));
                                    bPrice.add(row.getString("bPrice")
                                            + "£");
                                    bDescription.add(row
                                            .getString("bDescription"));
                                    bModule.add(row.getString("bModule"));
                                    bImage.add(Utils.PATH
                                            + row.getString("bImage"));
                                    list.add(row.getString("bImage"));
                                    // we check if this id already exists in the db, if it doesn't exists w create new one
                                    if (!db.hasIDbooks(row.getInt("bid")))
                                        db.createRowOnBooks(
                                                row.getInt("bid"),
                                                row.getString("bTitle"),
                                                row.getString("bCode"),
                                                row.getString("bPrice"),
                                                row.getString("bDescription"),
                                                row.getString("bModule"),
                                                Utils.PATH
                                                        + row.getString("bImage"),
                                                row.getString("bSpecialOffer"),
                                                row.getInt("bSpecialDiscount"),
                                                row.getString("bDateAdded"));
                                    Log.i(DEBUG,
                                            row.getString("bDescription"));
                                }
                                new DownloadImages(list, bAdapter)
                                        .execute();
                            }
                        }
                        postParameters.removeAll(postParameters);
                    } else {
                        Utils.IS_ENDED_PRODUCT_LIST = true;
                        if (rlLoading.isShown()) {
                            rlLoading.startAnimation(fadeOut());
                            rlLoading.setVisibility(View.INVISIBLE);
                        }
                    }
                } catch (Exception e) {
                    Log.e(DEBUG,
                            "Error at fillProductList(): " + e.toString());
                }
            }
        });
        task.execute();
    } else {
        // if we are not connected on internet or somehow the link would not work, then we will take the rows stored in sqlite db
        if (db.size(mySqlDatabase.TABLE_BOOKS) > 0) {
            Cursor cursor = db.getBookssRows(mySqlDatabase.TABLE_BOOKS);
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                bid.add(cursor.getInt(cursor
                        .getColumnIndex(mySqlDatabase.KEY_BID)));
                bTitle.add(cursor.getString(cursor
                        .getColumnIndex(mySqlDatabase.KEY_BTITLE)));
                bCode.add(cursor.getString(cursor
                        .getColumnIndex(mySqlDatabase.KEY_BCODE)));
                bPrice.add(cursor.getString(cursor
                        .getColumnIndex(mySqlDatabase.KEY_BPRICE)) + "£");
                bDescription.add(cursor.getString(cursor
                        .getColumnIndex(mySqlDatabase.KEY_BDESCRIPTION)));
                bModule.add(cursor.getString(cursor
                        .getColumnIndex(mySqlDatabase.KEY_BMODULE)));
                bImage.add(cursor.getString(cursor
                        .getColumnIndex(mySqlDatabase.KEY_BIMAGE)));
                cursor.moveToNext();
            }
            bAdapter.notifyDataSetChanged();
            Utils.IS_ENDED_PRODUCT_LIST = true;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

OnTaskCompleted之前

jarray = utils.getJarrayFromString(result);

使用

添加日志条目
Log.i(DEBUG,result);

我怀疑您获得的空值为result

if(result!="")

无法确保有效值。 干杯