从 Android 应用程序发送参数到服务器
我有一个 Android 应用程序,它从我的 mysql 数据库请求数据。这工作正常,但是当我尝试发送要检索的项目数的参数时,我什么也得不到。
这是我的 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));
?>
I have an android application that requests data from my mysql database. This works fine but when I try to send a parameter for how many items to retrieve I get nothing.
This is my java code:
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());
}
And this is my server side PHP code:
<?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));
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了。 while 循环中的条件不正确。
应该是
I solved it. The condition in the while-loop was incorrect.
It should be