简体   繁体   中英

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 java code:

                      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. 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. You should use the mysqli_ * *-functions or PDO ( http://php.net/manual/en/book.pdo.php ). mysql_query is deprecated as of PHP5.5 with a good reason. Especially when you post what seems to be a correct url to the above php-file.

If you are still having problems, please provide the content of the $json-variable, and a copy of the request from your Java-code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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