2
我有一個android應用程序,從我的mysql數據庫請求數據。這工作正常,但當我嘗試發送一個參數多少項檢索我什麼也沒有得到。從android應用程序發送參數到服務器
這是我的Java代碼:
result = "";
client = new DefaultHttpClient();
post = new HttpPost("http://www.XXX.XXXX.XX/XX.php");
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("itemsToGet", itemsToGet));
try {
post.setEntity((new UrlEncodedFormEntity(nameValuePairs)));
response = client.execute(post);
entity = response.getEntity();
inputStream = entity.getContent();
} catch (UnsupportedEncodingException e) {
Log.e(TAG, e.toString());
} catch (ClientProtocolException e) {
Log.e(TAG, e.toString());
} catch (IOException e) {
Log.e(TAG, e.toString());
}
try {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream ,"iso-8859-1"), 8);
stringBuilder = new StringBuilder();
String line = null;
while((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
inputStream.close();
result = stringBuilder.toString();
} catch (UnsupportedEncodingException e) {
Log.e(TAG, e.toString());
} catch (IOException e) {
Log.e(TAG, e.toString());
}
try {
JSONArray jsonArray = new JSONArray(result);
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
Log.i(TAG, object.getString("namn") + " " + i);
}
} catch (JSONException e) {
Log.e(TAG, e.toString());
}
這是我的服務器端的PHP代碼:
<?php
mysql_connect("XXX.XXX.com", "XXX", "XXX") or die(mysql_error());
mysql_select_db("XXXXXXX") or die(mysql_error());
$data = mysql_query("SELECT * FROM artikel") or die(mysql_error());
$itemsToGet = intval($_POST['itemsToGet']);
$counter = 0;
while($info = mysql_fetch_array($data) && $counter < $itemsToGet)
{
$databaseInfo[] = $info;
$counter++;
}
print(json_encode($databaseInfo));
>
你嘗試把你的PHP打印命令,看看你實際上得到連接?您是否通過除Android以外的其他方式驗證了此作品? – Jack
如果我不發送參數並遍歷整個數據庫,它可以正常工作。我可以打印出logcat中的所有數據。到目前爲止,我只用過它嘗試過,是的。 – plast