php - Android - 使用 JSON 和 httpurlconnect 发送数据

标签 php android mysql json

我正在尝试将数据从加速度计发送到 mysql 服务器(通过 php 脚本)。首先,我使用 DefaultHttpClient 做到了,但我只能发送有限的数据,我读到使用 HttpURLConnection 我可以发送大数据(我主要关心的),所以我决定更改我的代码以使用 HttpURLConnection 但我不知道我的JSONArray 已放置。有人可以帮忙吗?

旧代码:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.5/Android/vals.php");
......

JSONArray postjson=new JSONArray();
postjson.add(jsonx);
postjson.add(jsony);
postjson.add(jsonz);
....

 httppost.setHeader("json",jsonx.toString());
 httppost.setHeader("jsony",jsony.toString());
 httppost.setHeader("jsonz",jsonz.toString());
 httpclient.execute(httppost);

新代码:

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput (true);
                connection.setDoOutput (true);
                connection.setUseCaches (false);
                connection.setRequestMethod("POST"); 
                connection.setRequestProperty("Content-Type","application/json; charset=utf8");
                connection.connect();

我不知道应该在哪里设置 JSONArray 数据,就像我在下面的代码中所做的那样。谢谢。

编辑

最后我通过使用 DefaultHttpClient 实现了它,但我没有通过 header 发送 JSONArray,而是执行了以下操作:

        String jsonx = new Gson().toJson(param[0]); //This is inside asynctask
        String jsony = new Gson().toJson(param[1]);
        String jsonz = new Gson().toJson(param[2]);

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://192.168.1.5/Android/va.php");
         try
         {
               ArrayList<BasicNameValuePair> localArrayList = new ArrayList<BasicNameValuePair(); 
                localArrayList.add(new BasicNameValuePair("json",jsonx.toString()));
                localArrayList.add(new BasicNameValuePair("jsony",jsony.toString()));
                localArrayList.add(new BasicNameValuePair("jsonz",jsonz.toString()));
                try {
                    httppost.setEntity(new UrlEncodedFormEntity(localArrayList));
                    String str = EntityUtils.toString(httpclient.execute(httppost).getEntity());

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

如果有人遇到类似的问题并且我可以以某种方式提供帮助,我只是发布它。

最佳答案

使用android的http请求

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://url.com/file.php?a=abc&b=123&c=123");

            try {

                HttpResponse response = httpclient.execute(httppost);

                StatusLine statusLine = response.getStatusLine();
                System.out.println("new respos "
                        + statusLine.getStatusCode() + " "
                        + statusLine.toString());

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            }// process execption }
            catch (IOException e) {
                e.printStackTrace();
            }

php代码

 <?php
    $a= $_GET['a'];
    $b= $_GET['b'];
    $c= $_GET['c'];     
    ?>

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

相关文章:

php - 带有 INNER JOIN 的 SELECT 不起作用

java - 从 HttpResponse 获取 json

java - 结果集被重用

php - 将订单总重量添加到 WooCommerce 新订单电子邮件通知

javascript - ReactJS - 将发布数据传递到服务器

Android:从 fragment 中将 fragment 添加到 Activity

java - 在 Android 代码中访问网站并检索生成的图像

mysql - 如何选择一个空表,但在结果中获取一行(NULL + 数字)?

mysql - 如果未找到任何内容,则返回默认行

php - 散列密码 : hash() expects at least 2 parameters, 1 给定 APP/Model/Entity/User.php 时 CakePHP 3 出错