gpt4 book ai didi

java - Android应用程序和php之间发送和接收数据的问题

转载 作者:行者123 更新时间:2023-12-01 15:45:26 27 4
gpt4 key购买 nike

我在将数据从 Android 应用程序发送到 php 页面时遇到问题...我在 android 端有一个按钮,可以将我带到此 Activity :

public class Post extends Activity {

public void postData(View v){
//Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.67/phptester/insertdata.php");
List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(4);

try {
// Add your data
nameValuePairs.add(new BasicNameValuePair("id", "1"));
nameValuePairs.add(new BasicNameValuePair("descr", "Fire"));
nameValuePairs.add(new BasicNameValuePair("lat", "1.333"));
nameValuePairs.add(new BasicNameValuePair("lng", "1.333"));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
Log.i("postData", response.getStatusLine().toString());



} catch (IOException e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}
}

}

这是我的 php 代码:

  <?php
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db("locationdb", $conn);

if (isset($_POST["id"]) && isset($_POST['descr']) && isset($_POST['lat']) && isset($_POST['lng']))
{
$id = $_POST["id"];
$descr = $_POST["descr"];
$lat = $_POST["lat"];
$lon = $_POST["lon"];

$query = mysql_query("INSERT INTO location (ID, Description, lat, lng) VALUES('6', '$descr', '1.666', '1.666')");

echo "data inserted";
mysql_query($query);

}

else {

echo "Not data was intered ".mysql_error() ;
}

?>

困扰我的问题是该数据库中没有存储任何数据,我不知道为什么!!!! php 似乎无法从 Android 应用程序获取数据......感谢任何帮助,并提前致谢......

最佳答案

这不是解决方案,但可以帮助您查看服务器将哪些数据发送回您的应用,从而帮助您调试问题。

String response = httpclient.execute(httppost, new BasicResponseHandler());
Log.i("myapp", "Reply from server: " + response);

现在,您从 PHP 脚本中回显返回的任何内容都将打印在 Android 日志中,这可以让您更轻松地了解发生了什么情况。

关于java - Android应用程序和php之间发送和接收数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7137333/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com