JSON解析错误 - 字符串无法转换为JSONArray
问题描述:
我在运行我的Android应用程序时遇到问题。这是一个简单的活动,旨在通过HTTP从远程数据库获取数据。JSON解析错误 - 字符串无法转换为JSONArray
我得到的日志猫这两个错误:
错误解析成JSON的getJarrayFromString(); org.json.JSONException:Value类型为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;
}
}
}
答
在OnTaskCompleted
前
jarray = utils.getJarrayFromString(result);
添加日志条目与
Log.i(DEBUG,result);
我怀疑你得到一个空值result
if(result!="")
不确保有效的值。 干杯
+0
谢谢队友:)我从某人那里购买了这段代码,不幸的是我发现这里没有售后支持! :( 另外,你认为,那里需要有php脚本的名字吗?例如: if(result!=“books”);或者什么? 如果我通过电子邮件发送给你,你能看到我的代码吗?整个代码?请让我知道如果你可以吗?谢谢:) – user2123127 2013-03-01 10:59:56
您的代码很难阅读,并可能受益于某些格式。我们需要看到的真正方法是'getJarrayFromString(result)'。添加到你的问题。 – Perception 2013-03-01 10:46:40
还显示接收到的json字符串,即正在分析。 – Imperative 2013-03-01 10:47:20
谢谢你们,我发现这里很难以正确的格式粘贴。有没有其他方法可以做到这一点? :(我觉得真的很愚蠢 – user2123127 2013-03-01 10:51:39