json异常没有f1的值

时间:2015-05-15 03:29:49

标签: android

我已经创建了一个android应用程序,并尝试从数据库中选择数据到表行和textview。

但是当我试图处理json响应时,得到错误,这里是代码



 public void selectfromDb(){

        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://xxxxxxxx/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,"iso-8859-1"),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);

                    Log.i("log_tag","id: "+json_data.getInt("f1")+
                                    ", matier: "+json_data.getString("f2")
                    );

                    TextView b=new TextView(getActivity());
              //      String stime=String.valueOf(json_data.getInt("f1"));
                    b.setText(json_data.optString("f1"));
                    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("f2");
                    b1.setText(stime1);
                    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_SHORT).show();
        }

    }


  }
&#13;
&#13;
&#13;

这是代码php

&#13;
&#13;
    <?php

         $con = mysql_connect("","","");
         if (!$con)
           {
             die('Could not connect: ' . mysql_error());
           }

           mysql_select_db("databas", $con);

           $i=mysql_query("select * from test",$con);

           $num_rows = mysql_num_rows($i);
           $check='';
           while($row = mysql_fetch_array($i))
             {
                  $r[]=$row;
                  $check=$row['f1'];
             }
          
         if($check==NULL)
           {            
                  $r[$num_rows]="Record is not available";
                  echo json_encode($r);
           }
            else
             {
                $r[$num_rows]="success";
                 echo json_encode($r);
             } 

 mysql_close($con);
?> 
&#13;
&#13;
&#13; 的错误:

JSON exception no value for 'f1' 

1 个答案:

答案 0 :(得分:0)

这是php文件的输出

[{"0":"66","id":"66","1":"","matier":""},{"0":"65","id":"65","1":"","matier":""},{"0":"64","id":"64","1":"","matier":""},{"0":"63","id":"63","1":"","matier":""},{"0":"62","id":"62","1":"","matier":""},{"0":"61","id":"61","1":"","matier":""},{"0":"60","id":"60","1":"","matier":""},{"0":"59","id":"59","1":"","matier":""},{"0":"58","id":"58","1":"","matier":""},{"0":"57","id":"57","1":"","matier":""},{"0":"56","id":"56","1":"","matier":""},{"0":"55","id":"55","1":"","matier":""},{"0":"54","id":"54","1":"test","matier":"test"},{"0":"53","id":"53","1":"jfhhf","matier":"jfhhf"},{"0":"52","id":"52","1":"jfhhf","matier":"jfhhf"},{"0":"42","id":"42","1":"ykk","matier":"ykk"},{"0":"51","id":"51","1":"jfhhf","matier":"jfhhf"},{"0":"50","id":"50","1":"jfhhf","matier":"jfhhf"},{"0":"49","id":"49","1":"opp","matier":"opp"},{"0":"48","id":"48","1":"opp","matier":"opp"},{"0":"47","id":"47","1":"opp","matier":"opp"},{"0":"46","id":"46","1":"opp","matier":"opp"},{"0":"45","id":"45","1":"opp","matier":"opp"},{"0":"44","id":"44","1":"ykfvjf","matier":"ykfvjf"},{"0":"43","id":"43","1":"ykfvjf","matier":"ykfvjf"},"Record is not available"]