解析数据时出错org.json.JSONException:Value

时间:2015-05-18 00:25:36

标签: android

Iam wrte一个Android应用程序,并尝试从数据库中选择数据并输入到tablerow和textview。

但是当我尝试处理json响应时,就像

方法从数据库中选择

    public void selectfromDb(){



        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://tijarati.ma/webservices/selectall.php");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

            Log.e("log_tag", "connection success ");
            //   Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error in http connection "+e.toString());
            Toast.makeText(getActivity(), "Connection fail", Toast.LENGTH_SHORT).show();

        }
        //convert response to string
        try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
                //  Toast.makeText(getApplicationContext(), "Input Reading pass", Toast.LENGTH_SHORT).show();
            }
            is.close();

            result=sb.toString();
        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error converting result "+e.toString());
            Toast.makeText(getActivity(), " Input reading fail", Toast.LENGTH_SHORT).show();

        }

        //parse json data
        try
        {

            JSONArray jArray = new JSONArray(result);


            String re=jArray.getString(jArray.length()-1);


            TableLayout tv=(TableLayout)getView().findViewById(R.id.table);
            tv.removeAllViewsInLayout();




            int flag=1;

            for(int i=-1;i<jArray.length()-1;i++)

            {




                TableRow tr=new TableRow(getActivity());

                tr.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));




                if(flag==1)
                {

                    TextView b6=new TextView(getActivity());
                    b6.setText("ID");
                    b6.setTextColor(Color.BLUE);
                    b6.setTextSize(15);
                    tr.addView(b6);


                    TextView b19=new TextView(getActivity());
                    b19.setPadding(10, 0, 0, 0);
                    b19.setTextSize(15);
                    b19.setText("Matier");
                    b19.setTextColor(Color.BLUE);
                    tr.addView(b19);




                    tv.addView(tr);

                    final View vline = new View(getActivity());
                    vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
                    vline.setBackgroundColor(Color.BLUE);



                    tv.addView(vline);
                    flag=0;


                }

                else
                {



                    JSONObject json_data = jArray.getJSONObject(i);
                  int id=json_data.getInt("id");
                    String namematier=json_data.getString("matier");

                    Log.i("log_tag","id: "+id+
                                    ", matier: "+namematier
                    );




                    TextView b=new TextView(getActivity());
              //      String stime=String.valueOf(json_data.getInt("f1"));
                    if(json_data.has("f")) {
                        b.setText(id);
                        b.setTextColor(Color.RED);
                        b.setTextSize(15);
                        tr.addView(b);

                    }
                    TextView b1=new TextView(getActivity());
                    b1.setPadding(10, 0, 0, 0);
                    b1.setTextSize(15);
                    String stime1=json_data.optString(namematier);
                    b1.setText(namematier);
                    b1.setTextColor(Color.WHITE);
                    tr.addView(b1);


                    tv.addView(tr);


                    final View vline1 = new View(getActivity());
                    vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                    vline1.setBackgroundColor(Color.WHITE);
                    tv.addView(vline1);


                }

            }



        }
        catch(JSONException e)
        {
            Log.e("log_tag", "Error parsing data "+e.toString());
            Toast.makeText(getActivity(),  e.toString(), Toast.LENGTH_LONG).show();
        }

代码php

 $result = mysql_query("SELECT * FROM adminmatier") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {



    $response["matier"] = array();

    while ($row = mysql_fetch_array($result)) {


        $product = array();
        $product["id"] = $row["id"];
        $product["matier"] = $row["matier"];


        // push single product into final response array
        array_push($response["matier"], $product);
    }
    // success
    $response["success"] = 1;


    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No products found";


    echo json_encode($response);
}
?>

这是logcat

 Error parsing data org.json.JSONException: Value {"matier":[{"id":"116","matier":""},{"id":"115","matier":""},{"id":"114","matier":""},{"id":"113","matier":""},{"id":"112","matier":""},{"id":"111","matier":"azert"},{"id":"110","matier":"azert"},{"id":"109","matier":"hdhdhe"},{"id":"108","matier":""},{"id":"107","matier":""},{"id":"106","matier":""},{"id":"105","matier":""},{"id":"104","matier":""},{"id":"103","matier":""},{"id":"102","matier":""},{"id":"101","matier":"hkcj"},{"id":"100","matier":"hkcj"},{"id":"99","matier":"hkcj"},{"id":"98","matier":"hkcj"},{"id":"97","matier":"hkcj"},{"id":"96","matier":"hkcj"},{"id":"95","matier":"hkcj"},{"id":"94","matier":"hkcj"},{"id":"93","matier":"hkcj"},{"id":"92","matier":"g"},{"id":"91","matier":""},{"id":"90","matier":""},{"id":"89","matier":"bilal"},{"id":"88","matier":"bilal"},{"id":"84","matier":"test"},{"id":"85","matier":"tr"},{"id":"86","matier":"jdjjf"},{"id":"87","matier":"ghftu"},{"id":"118","matier":""},{"id":"117","matier":""}],"success":1} of type org.json.JSONObject cannot be converted to JSONArray

当我在这个网站上测试结果json时

http://jsonformatter.curiousconcept.com/

网址

http://tijarati.ma/webservices/selectall.php

VALID解析json

我知道问题在哪里

1 个答案:

答案 0 :(得分:0)

您的响应JSON是一个JSON对象,它不是JSON数组。

因此,请尝试从

更改代码
JSONArray jArray = new JSONArray(result);

JSONObject result = new JSONObject(result);

JSONArray jArray =  result.getJSONArray("matier");