我面临的问题是,我想从运行PHP和MySQL的服务器返回一个JSON数组到android应用程序。我没有得到任何错误,但是代码并没有按照需要给出输出。执行我的代码后,我得到以下输出。获取“[]”作为PHP服务器应答,应该返回一个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:[email protected]
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值。帮助我,谢谢你。
http://stackoverflow.com/questions/21131015/how-to-send-a-string -array-as-basic-name-value-pair-as-httppost – goonerDroid 2014-09-26 11:04:56
确保你的查询返回任何结果。 – Rorschach 2014-09-26 11:05:16
那么你有没有测试过的PHP?因为'[]'是空数组的json,可能你的sql没有返回任何结果 – Steve 2014-09-26 11:05:26