将数据从Java发送到PHP脚本。尝试了一切。但遗漏了一件小事

时间:2013-12-05 07:53:37

标签: java php android json

我尝试了一切,但无法从Android应用程序成功输入数据库中的数据。

我的Java代码是:

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    String js = "{\"1\":\"9999\",\"2\":\"test\",\"3\":\"AJE\",\"4\":\"Kutch\",\"5\":\"200000\",}";
    String respons;
    String par;
    @Override
    protected String doInBackground(String... urls){
        try {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);

            StringEntity params = new StringEntity("message=" + js);
            par = params.toString();
            httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
            httppost.setEntity(params);
            HttpResponse response = httpclient.execute(httppost);
            Log.i("postData", response.getStatusLine().toString());
            respons = response.getStatusLine().toString();
            // Could do something better with response.
        } catch (Exception e) {
            Log.e("log_tag", "Error:  " + e.toString());
        }
        return "1";
    }

我的PHP脚本如下:

<?php

  $jstr=$_POST['message'];

  $jsn = json_decode($jstr,true);

  $con=mysqli_connect("localhost","root","123456","world");

  if (mysqli_connect_errno())
 {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }else
 {
   echo "Connected";
 }

$qr = "INSERT INTO city VALUES (\"$jsn[0]\",\"$jsn[1]\",\"$jsn[2]\",\"$jsn[3]\",\"$jsn[4]\")";

echo $qr;
mysqli_query($con,$qr);

if (!mysqli_query($con,$qr))
{
  die('Error: ' . mysqli_error($con));
}

echo "1 record added";

mysqli_close($con);

?>

抱歉,这可能是提出的许多类似问题之一。但我已尝试过所有事情,然后我发布了这个问题。我似乎需要特定于代码的解决方案。

0 个答案:

没有答案