得到";[]”;作为来自php服务器的响应,该服务器应该返回一个JSON数组


Getting "[ ]" as response from php server which is supposed to return a JSON array

我面临的问题是,我想从运行PHP和MySQL的服务器向android应用程序返回一个JSON数组。我没有得到任何错误,但代码没有给出所需的输出。在执行代码后,我得到了以下输出。

Logcat条目:

09-26 15:53:54.166: I/System.out(32008): Connected
09-26 15:53:54.226: D/MyActivity(32008): registered
09-26 15:53:54.266: D/dalvikvm(32008): GC_FOR_ALLOC freed 254K, 2% free 16955K/17240K, paused 10ms, total 10ms
09-26 15:53:54.266: I/System.out(32008): Process Result started
09-26 15:53:54.496: I/System.out(32008): 200
09-26 15:53:54.496: I/System.out(32008): BR REader:java.io.BufferedReader@426d40e0
09-26 15:53:54.536: I/System.out(32008): String builder:[]  
09-26 15:53:54.536: I/System.out(32008): Result:[]  
09-26 15:53:54.536: I/System.out(32008): Process Result ended
09-26 15:53:54.566: D/dalvikvm(32008): GC_FOR_ALLOC freed 253K, 2% free 17216K/17500K, paused 13ms, total 14ms
09-26 15:53:54.586: I/Adreno-EGL(32008): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
09-26 15:53:54.616: D/OpenGLRenderer(32008): Enabling debug mode 0


源代码是:

protected void onCreate(Bundle savedInstanceState) {
    ....................
    ....................
    syncDatabases = new SyncDatabases();
    try {
        syncDatabases.execute().get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}
public class SyncDatabases extends AsyncTask<Void, Void, String> {
    String result=null;
    @Override
    protected String doInBackground(Void... String) {
        try {
            System.out.println("Process Result started");
            String result1=getJSONUrl(uploadDetailServerUri);
            System.out.println("Result:"+result1);
            JSONArray json=new JSONArray(result1);
            for (int i = 0; i < json.length(); i++) {
            JSONObject jsonOb=json.getJSONObject(i);
            System.out.println("JSON Colection:"+jsonOb.toString());
            String imageName = jsonOb.getString("imagename");
            String status = jsonOb.getString("status");
            System.out.println("ImageName:"+imageName+"Status:"+status);
            }
            System.out.println("Process Result ended");
            } catch (Exception e) {
            e.printStackTrace();
            }
        return null;
    }
    public String getJSONUrl(String url) {
        StringBuilder str = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        // HttpGet httpGet = new HttpGet(url);
        HttpPost httpPost = new HttpPost(url);
        // httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json; charset=utf-8");
        List<NameValuePair> nameVP = new ArrayList<NameValuePair>(1);
            nameVP.add(new BasicNameValuePair("mobile_number", phoneNumber));
            try {   
            httpPost.setEntity(new UrlEncodedFormEntity(nameVP));
            //  httpGet.setParams(new BasicNameValuePair("mobile_number", phoneNumber));
            HttpResponse response = client.execute(httpPost);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            System.out.println(statusCode);
            if (statusCode == 200) { // Download OK
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content,"UTF-8"));
            System.out.println("BR REader:"+reader.toString());
            String line;
            while ((line = reader.readLine()) != null) {
            str.append(line);
            }
            } else {
            Log.e("Log", "Failed to download file..");
            }
            } catch (ClientProtocolException e) {
            e.printStackTrace();
             } catch (IOException e) {
            e.printStackTrace();
             }
            System.out.println("String builder:"+str.toString());
             return str.toString();
     }

    }

PHP代码是:

<?php
include ('mysqlconnection.php');
$mobile =  $_POST['mobile_number'];
$output = array();
$query="select name,status from content;";
$sql = mysql_query($query);
 while($row = mysql_fetch_assoc($sql)) {
    $output[]= array( 'imagename' => $row['name'], 'status' => $row['status'] );
}
echo json_encode($output);
?>

当从浏览器发送httprequest时,我们在浏览器中得到了所需的输出(JSON数组)。

我不知道为什么我在logcat中只得到"[]"作为输出,而不是打印JSONArray值。帮帮我,谢谢你。

我觉得你在使用保留字,请更改你的查询

$query="select name,status from content;";

$query="select `name`,`status` from content";