我试图将json发送到php并存储发送的数据
但我做不到
我的Android代码

  public void sendToServer(String txt) throws JSONException{

            String path = "http://10.0.0.6:8888/json.php";

            HttpClient client = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
                                                                                    // Limit
            HttpResponse response;
            JSONObject json = new JSONObject();

            try {
                HttpPost post = new HttpPost(path);
                json.put("text", txt);
                Log.i("jason Object", json.toString()); //This print the data perfectly
                post.setHeader("json", json.toString());
                post.setHeader("Accept", "application/json");

                Log.i("Done", "DONE 1");

                StringEntity se = new StringEntity(json.toString());

                se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                        "application/json"));
                Log.i("Done", "DONE 2");

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("myjson", json.toString()));
                post.setEntity(new UrlEncodedFormEntity(nameValuePairs));


                Log.i("Done", "DONE 3");

                response = client.execute(post);

                /* Checking response */
                if (response != null) {
                    InputStream in = response.getEntity().getContent(); // Get the
                                                                        // data in
                                                                            // the
                                                                            // entity
                    String a = convertStreamToString(in);
                    Log.i("Read from Server", a);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
                     }

以及我的php代码
<?php

$decoded = json_decode(stripslashes($_POST['myjson']));
if($decoded)
$con = mysql_connect('localhost','root','root')
       or die('Cannot connect to the DB');
mysql_select_db('deaf',$con);
mysql_query("INSERT INTO text
VALUES ('".$decoded->{'text'}"')");
mysql_close($con);
$posts = array(1);
 header('Content-type: application/json');
 echo json_encode(array('posts'=>$posts));

else
echo "error";
  ?>

insert语句不起作用,我花了好几个小时试图修复它,但什么也没起作用
请帮帮我,谢谢

最佳答案

这有一些问题。
您似乎只是将json塞进代码的任意部分(json头、从未使用过的stringentity、myjson url编码的表单字段)。我建议简单地将请求的主体设置为json文档。但是,还有其他方法,所以让我们尝试让myjson工作。如果你这么做了,我会把名字改成类似document的名字。它不需要在头中,您可以删除stringentity。
stripslashes的正确用法很少。您不必在表单输入上运行它。如果这样做,可能意味着启用了magic_quotes。magic_引号已从php(5.4及更高版本)中删除,不应使用。
我建议你用准备好的陈述。否则,应使用mysql_real_escape_string。否则,您将易受SQL注入攻击。
您可以简化对象访问。所以代码应该是:
γ
mysql_query("INSERT INTO text VALUES ('" . mysql_real_scape_string($decoded->text) . "')");
如果insert语句失败,请发布错误,包括mysql_error

关于php - 使用Android将JSON发送到PHP,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9934226/

10-12 12:26
查看更多