-5

我在运行我的 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;
        }
    }
}
4

1 回答 1

1

OnTaskCompleted之前

jarray = utils.getJarrayFromString(result);

添加一个日志条目

Log.i(DEBUG,result);

我怀疑你得到一个空值result

if(result!="")

不确保有效值。干杯

于 2013-03-01T10:54:58.980 回答