【问题标题】:Sending JSON to PHP using Android使用 Android 向 PHP 发送 JSON
【发布时间】:2012-03-29 22:01:59
【问题描述】:

我正在尝试将 JSON 发送到 php 并存储发送的数据 但我不能让它工作

我的安卓代码

  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";
  ?>

插入语句不起作用,我花了几个小时试图修复它,但没有任何效果 请帮助我,谢谢

【问题讨论】:

  • 您在 Java 的 in 变量中得到了什么?用if {} else {} 替换您的模板if。它只适用于 1 行代码,如果你在这里有 10 行代码。这行不通。
  • 对不起,这些是从服务器读取的,不是发送给它的,我发送有问题,谢谢

标签: php android json post


【解决方案1】:

这有一些问题。

  1. 您似乎只是将 json 塞入代码的任意部分(json 标头、从未使用过的 StringEntity、myjson url 编码的表单字段)。我建议简单地将请求的正文作为 JSON 文档。但是,还有其他方法,所以让我们尝试让myjson 工作。如果您这样做,我会将名称更改为 document 之类的名称。它不需要在标题中,您可以删除 StringEntity。
  2. stripslashes 的正确用法很少。您不必在表单输入上运行它。如果你这样做了,那可能意味着magic_quotes 已启用。 magic_quotes 已从 PHP(5.4 及更高版本)中删除,不应使用。
  3. 我建议您使用准备好的语句。如果没有,您应该使用mysql_real_escape_string。否则,您将容易受到 SQL 注入的攻击。
  4. 您可以简化对象访问。所以代码应该是:

    mysql_query("INSERT INTO text VALUES ('" . mysql_real_scape_string($decoded-&gt;text) . "')");

如果插入语句失败,请贴出错误是什么,包括mysql_error

【讨论】:

  • 感谢回复,只是一个问题,myjson或者document,如果我使用json对象会怎么样??
  • 我是说在客户端和服务器上都用document 替换myjson
  • 我知道,但是 JSON 会是什么样子 {"text": "sometext"} 该对象中的文档在哪里,或者仅用于处理?对不起,但我对此真的很陌生:)
  • 你使用的是 UrlEncodedFormEntity,所以它就像document={"text": "sometext"},除了等号后面的部分是 url 编码的。
猜你喜欢
  • 2013-01-22
  • 2013-11-05
  • 1970-01-01
  • 1970-01-01
  • 2018-06-30
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 2015-07-21
相关资源
最近更新 更多