Php Json数组为null,而jsonobject具有该值

Php Json数组为null,而jsonobject具有该值,php,android,arrays,Php,Android,Arrays,当检索myjson字符串时,我使用json将数据从php检索到android,当我使用 array= j.getJSONArray("result"); j有第一个数组元素,但我的数组为空。 有人能告诉我这背后的确切原因吗 public void getData() { class GetDataJSON extends AsyncTask<String, Void, String> { @Override

当检索myjson字符串时,我使用json将数据从php检索到android,当我使用

array= j.getJSONArray("result"); 
j有第一个数组元素,但我的数组为空。 有人能告诉我这背后的确切原因吗

    public void getData() {
        class GetDataJSON extends AsyncTask<String, Void, String> {

            @Override
            protected String doInBackground(String... params) {
                DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                HttpPost httppost = new HttpPost("http://futuroitsolutions.in/php_android_api/data1.php");

                // Depends on your web service
                httppost.setHeader("Content-type", "application/json");

                InputStream inputStream = null;
                String result = null;
                try {
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();

                    inputStream = entity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (Exception e) {
                    // Oops
                } finally {
                    try {
                        if (inputStream != null) inputStream.close();
                    } catch (Exception squish) {
                    }
                }
                return result;
            }

            @Override
            protected void onPostExecute(String result) {
                myJSON = result;
                ShowList( );
            }
        }
        GetDataJSON g = new GetDataJSON();
        g.execute();

    }
 public void ShowList() {
try {

    String vi=myJSON;
    vi=vi.replace('[',' ').replace(']',' ');
      JSONObject j = new JSONObject(vi);
      array= j.getJSONArray("result");
       try {


        for(int i=0;i<array.length();i++){
            JSONObject c = array.getJSONObject(i);
            id = c.getString(TAG_ID);
            name = c.getString(TAG_NAME);
            km = c.getString(TAG_KILOMETER);
            time = c.getString(TAG_TIMING);
            // get your data from jsonobject
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

        //    String regno = c.getString(TAG_REG);
        //   String phone = c.getString(TAG_PHONE);
        //   String category = c.getString(TAG_CAT);

    }

        catch (JSONException e) {
            e.printStackTrace();
        }
     TableLayout tv = (TableLayout) findViewById(R.id.table);
      tv.removeAllViewsInLayout();
        int flag = 1;
     int c=array.length();
        for (int i = 0; i < c ; i++) {
        TableRow tr = new TableRow(MainActivity.this);
        tr.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        if (flag == 1) {
            TextView b6 = new TextView(MainActivity.this);
            b6.setText("Id");
            b6.setTextColor(Color.BLUE);
            b6.setTextSize(15);
            tr.addView(b6);
            TextView b19 = new TextView(MainActivity.this);
            b19.setPadding(10, 0, 0, 0);
            b19.setTextSize(15);
            b19.setText("Task name");
            b19.setTextColor(Color.BLUE);
            tr.addView(b19);
            TextView b29 = new TextView(MainActivity.this);
            b29.setPadding(10, 0, 0, 0);
            b29.setText("Timing");
            b29.setTextColor(Color.BLUE);
            b29.setTextSize(15);
            tr.addView(b29);
            tv.addView(tr);
            TextView b21 = new TextView(MainActivity.this);
            b29.setPadding(10, 0, 0, 0);
            b29.setText("Kilometers");
            b29.setTextColor(Color.BLUE);
            b29.setTextSize(15);
            tr.addView(b21);
            tv.addView(tr);

            final View vline = new View(MainActivity.this);
            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("Id") + ", Username: " + json_data.getString("Task name") + ", No: " + json_data.getString("Kilometers"));
            TextView b = new TextView(MainActivity.this);
            String stime =id;
            b.setText(stime);
            b.setTextColor(Color.RED);
            b.setTextSize(15);
            tr.addView(b);
            TextView b1 = new TextView(MainActivity.this);
            b1.setPadding(10, 0, 0, 0);
            b1.setTextSize(15);
            String stime1 = name;
            b1.setText(stime1);
            b1.setTextColor(Color.BLACK);
            tr.addView(b1);
            TextView b2 = new TextView(MainActivity.this);
            b2.setPadding(10, 0, 0, 0);
            String stime2 = time;
            b2.setText(stime2);
            b2.setTextColor(Color.BLACK);
            b2.setTextSize(15);
            tr.addView(b2);
            tv.addView(tr);
            TextView b3 = new TextView(MainActivity.this);
            b2.setPadding(10, 0, 0, 0);
            String stime3 = km;
            b2.setText(stime3);
            b2.setTextColor(Color.BLACK);
            b2.setTextSize(15);
            tr.addView(b2);
            tv.addView(tr);
            final View vline1 = new View(MainActivity.this);
            vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
            vline1.setBackgroundColor(Color.WHITE);
            tv.addView(vline1);
        }
    }
}
数组=空 我的json

[{id:1,任务名:edapally to kakkanad,SourceLa:10.0236761,SourceLo:76.311623,Path1La:10.0327153,Path1Lo:76.331889,Path2La:10.0231412,Path2Lo:76.3409954,DestinationLa:10.0158605,DestinationLo:76.3418666,时间:上午10点,公里:8}]

Data1.php


您尚未在while循环之外定义变量$rows。这就是$rows的作用域未定义的原因

<?php
include('sql.php');
mysql_query('SET CHARACTER SET utf8') ;
$rows=array();
$result = mysql_query('SELECT * FROM Location') or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
     $rows[] = $row;
}
$final=array("result"=>$rows);
echo json_encode($rows);
试试这个

String vi=myJSON;
JSONArray array = new JSONArray(vi);

您可以尝试以下代码:

JSONArray jsonArray = new JSONArray(vi);
                    for(int k = 0; k < jsonArray.length(); k++)
                    {
                        JSONObject jsonObject = jsonArray.getJSONObject(k);

                        jsonObject.getString("id");
                        jsonObject.getString("Taskname");

                        .............
                        // parse all fields like above
                    }

你得到的是JSONObject,而不是你说的vi中的JSONArray。如果你想要结果,你应该在php中编码$final,而不是$rows。我只编码final,但它也给出了数组null,所以只有我编码了$rows,而且当我编码时,我得到了类似于{results………[…]的json。给我你的json对象你还在这样做吗?vi=vi.替换“[”,''。替换“]','';如果是,请删除该行。我正在获取数组,但其长度似乎为空
String vi=myJSON;
JSONArray array = new JSONArray(vi);
JSONArray jsonArray = new JSONArray(vi);
                    for(int k = 0; k < jsonArray.length(); k++)
                    {
                        JSONObject jsonObject = jsonArray.getJSONObject(k);

                        jsonObject.getString("id");
                        jsonObject.getString("Taskname");

                        .............
                        // parse all fields like above
                    }