我面臨的問題是,我想從運行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