Java JSON异常错误

Java JSON异常错误,java,php,android,json,parsing,Java,Php,Android,Json,Parsing,运行应用程序时,我在logcat上遇到以下错误。我之前有很多错误,在做了一些研究之后,我设法解决了这些错误。然而,这一点我无法理解: 03-04 02:52:29.475: E/JustDealsUtils(1913): Error parsing to json on getJarrayFromString(); org.json.JSONException: Expected ':' after result at character 8 of {result} 编辑1: 现在的错误是:

运行应用程序时,我在logcat上遇到以下错误。我之前有很多错误,在做了一些研究之后,我设法解决了这些错误。然而,这一点我无法理解:

03-04 02:52:29.475: E/JustDealsUtils(1913): Error parsing to json on getJarrayFromString(); org.json.JSONException: Expected ':' after result at character 8 of {result}
编辑1:

现在的错误是:

Error parsing to json on getJarrayFromString(); org.json.JSONException: Value result of type java.lang.String cannot be converted to JSONArray
与此相关的还有fillproductlist()的错误

以下是我的Java代码:

public void onTaskCompleted(String result) {
                        try {  
                            if(result!=""){
                                // the remote php link 
                                // converting the response into json array
                                Log.i(DEBUG, result);
                                jarray = utils.getJarrayFromString(result);

                                // number of rows in total for a query
                                int mysqlSize = (jarray.getJSONObject(0).getInt("numRows"));

                                Log.i(DEBUG, "From " + from + " to " + mysqlSize);

                                // to check if all the rows are parsed from the mysql
                                if(from <= mysqlSize){
                                    int rows;
                                    // to check 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"));
                                            // to check if an id already exists in the db or to create one if doesn't exist
                                            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 internet connectio is not available
            // then, rows will be fetched from the local sqllite database stored on the android phone
            if(db.size(justdealsDatabase.TABLE_BOOKS) > 0){
                Cursor cursor = db.getBooksRows(justdealsDatabase.TABLE_BOOKS);
                cursor.moveToFirst();
                while(!cursor.isAfterLast()){
                    bid.add(cursor.getInt(cursor.getColumnIndex(justdealsDatabase.KEY_BID)));
                    bTitle.add(cursor.getString(cursor.getColumnIndex(justdealsDatabase.KEY_BTITLE)));
                    bCode.add(cursor.getString(cursor.getColumnIndex(justdealsDatabase.KEY_BCODE)));
                    bPrice.add(cursor.getString(cursor.getColumnIndex(justdealsDatabase.KEY_BPRICE))+ "£");
                    bDescription.add(cursor.getString(cursor.getColumnIndex(justdealsDatabase.KEY_BDESCRIPTION)));
                    bModule.add(cursor.getString(cursor.getColumnIndex(justdealsDatabase.KEY_BMODULE)));
                    bImage.add(cursor.getString(cursor.getColumnIndex(justdealsDatabase.KEY_BIMAGE)));
                    cursor.moveToNext();
                }
                bAdapter.notifyDataSetChanged();
                Utils.IS_ENDED_PRODUCT_LIST = true;
            }
        }
    }
和我的PHP API(编辑1):


我已经实现了你们推荐的所有建议,但这段代码仍然没有成功

我不知道为什么“RESULT”字符串的值不能转换成JSONARRAY


我已经向您展示了JSON数组声明、结果字符串声明以及PHP(编辑版本见上文)

您必须使用
echo
而不是
print\r

echo json_encode($rows);

print\r
以数组格式提供输出。

您必须发送两次结果,您必须仅发送一次编码的结果 请删除以下代码并重试

echo json_encode($rows);

也许您应该命名不带“{}”符号的数组

而不是

jarray = new JSONArray("{result}");

{result}.length==8,这就是您在字符8中得到的错误。应该是
jarray=newjsonarray(结果)@user1516873。我又收到20个错误,应用程序意外终止!!哦,这只是意味着你的代码中有很多bug。例如,
echo json_encode($rows)。这条线到底是干什么的?我想它不应该出现在这里。我已经删除了它,但它仍然不能正常工作,下一件事-为什么要运行查询2次?删除第一个select并将数组定义移动到第二个下面
$query=$db->query($sql)
$sql.=“限制$from,$nr”将引发SQL异常,应该是
$SQL.=“LIMIT$from,$nr”下一步:
打印(json编码($rows))应该是
echo json\u encode($rows)谢谢@Yogesh Suthar,但这没有帮助。它仍然在LogCat中给我同样的错误<代码>预期“:”在{result}的字符8处的结果之后
@TirathSingh您必须删除
echo json_encode($rows)
header()…
之后。如果在web浏览器中测试php API,我的JSON输出为“null”。我用echo代替了print_r,也遵循了上面的步骤,但仍然不走运!!同样的错误@TirathSingh您必须删除
echo json\u encode($rows)
标题()之后..
。然后检查json输出。@Thirumalai murugan我已经删除了它。。我粘贴了上面错误的代码版本。在我的工作代码中,它已经在标题后被删除。。还是没有运气@Tirath Singh你能给我们新的版本代码吗?这样我们就可以检查并尝试使用echo来代替print\r使用echo整个代码都很好只是我在header语句之后去掉了echo语句,print\r被更改为echo。这是allI希望您熟悉firebug,请查看ajax返回,然后进行检查,给我们返回文本进行检查,然后您也可以尝试这个jarray=new JSONArray(结果);谢谢你,安德烈。我试过了。错误仍然存在。然而,这个时间错误有点不同:
在getJarrayFromString()上解析json时出错;org.json.JSONException:java.lang.String类型的值结果无法转换为JSONArray
echo json_encode($rows);
jarray = new JSONArray("result");
jarray = new JSONArray("{result}");