简体   繁体   English

使用json将数据从android发送到php

[英]sending data from android to php using json

hey im trying to send data from my android application to a web server and send it to the database using mysql, altough it doesn't seems to have any errors, my code doesn't work嘿,我正在尝试将数据从我的 android 应用程序发送到 Web 服务器并使用 mysql 将其发送到数据库,尽管它似乎没有任何错误,但我的代码不起作用

android java code:安卓Java代码:

                      String categorys=category.getText().toString();
          String authors = author.getText().toString(); 
          String quess = question.getText().toString(); 
          String anss = answer.getText().toString();    

                    try {
                        JSONObject json = new JSONObject(); 
                        json.put("category",categorys); 
                        json.put("ques",quess);
                        json.put("ans",anss);
                        json.put("authors",authors);
                        postData(json);


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });     
        }

        public void postData(JSONObject json) throws JSONException {
            HttpClient httpclient = new DefaultHttpClient();

            try { 
                HttpPost httppost = new HttpPost("http://shlomo.webuda.com/androidtomy.php");

                List<NameValuePair> nvp = new ArrayList<NameValuePair>(2);    
                nvp.add(new BasicNameValuePair("json", json.toString()));
                //httppost.setHeader("Content-type", "application/json");  
                httppost.setEntity(new UrlEncodedFormEntity(nvp));
                HttpResponse response = httpclient.execute(httppost); 

                if(response != null) {
                    InputStream is = response.getEntity().getContent();
                    //input stream is response that can be shown back on android
                }

            }
            catch (Exception e) 
            {
                e.printStackTrace();
            } 




                }`

php code代码

mysql_connect("something","something","something");
mysql_select_db("something");
 $json = $_SERVER['HTTP_JSON'];
 echo "JSON: \n";
 var_dump($json);
 echo "\n\n";

 $data = json_decode($json,true);
 var_dump($data);




$category=$data['category'];
$author=$data['authors'];
$question=$data['ques'];
$answer=$data['ans'];
$sql = 'INSERT INTO Ques(Ques_Author, Ques_Question,Ques_Answer,Ques_Category,Ques_Approve) values("category","author","question","answer","0")';
mysql_query($sql);

}    

?> ?>

As noted in the comments the PHP-file has several issues.正如评论中所指出的,PHP 文件有几个问题。 Try the below instead:请尝试以下方法:

mysql_connect("something","something","something");
mysql_select_db("something");


$json = $_REQUEST['json'];
echo "JSON: \n";
var_dump($json);
echo "\n\n";

$data = json_decode($json,true);
var_dump($data);

$category=$data['category'];
$author=$data['authors'];
$question=$data['ques'];
$answer=$data['ans'];
$sql = "INSERT INTO Ques(Ques_Author, Ques_Question,Ques_Answer,Ques_Category,Ques_Approve) values($category,$author,$question,$answer,0)";
mysql_query($sql);

Also you should not use the mysql_query-function.此外,您不应使用 mysql_query 函数。 You should use the mysqli_ * *-functions or PDO ( http://php.net/manual/en/book.pdo.php ).您应该使用 mysqli_ * *-functions 或 PDO ( http://php.net/manual/en/book.pdo.php )。 mysql_query is deprecated as of PHP5.5 with a good reason. mysql_query 从 PHP5.5 开始被弃用是有充分理由的。 Especially when you post what seems to be a correct url to the above php-file.特别是当您将似乎正确的 url 发布到上述 php 文件时。

If you are still having problems, please provide the content of the $json-variable, and a copy of the request from your Java-code如果您仍然遇到问题,请提供 $json 变量的内容,以及来自您的 Java 代码的请求副本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM