Android - 使用JSON和httpurlconnect发送数据

时间:2014-11-11 09:45:05

标签: 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实现了它,但不是通过标头发送我的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();
                }

如果有人有类似的麻烦,我就发布它,我可以以某种方式提供帮助。

1 个答案:

答案 0 :(得分:0)

使用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 code

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