使用Android的Jsoup将数据发送到服务器 [英] Sending Data to server using Jsoup Android

查看:184
本文介绍了使用Android的Jsoup将数据发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下面的数据发送到服务器

I am trying to send the following data to server

{"feedback":{"q1":12.0, "q2":33, "q3":12.0, "q4":78, "q5":12.0, "q6":33, "q7":12.0, "q8":33, "q9":12.0, "q10":33, "latitude":12.0, "longitude":33.08, "imei":128790, "time":"base64string"}}

我无法创建合适的jsoup字符串。我能够连接到我的服务器,但是当我尝试发送邮件使用 POST 请求以下信息。它失败。

I am unable to create a proper jsoup String. i am able to connect to my server but when i try to send the following information using POST request. it fails.

我想通过我的Web控制台发送相同的请求,它工作得很好那里。

I tried to send the same request through my WEB CONSOLE, and it worked fine there as well.

下面是我的code:

      protected String doInBackground(String... urls) {

                try {
                    // Connect to the web site
                    Document document = Jsoup.connect("http://107.170.91.100/books/save_feedback")
                            .data("q1", q1).data("q2", q2).data("q3", q3).data("q4", q4).data("q5", q5).data("q6", q6).data("q7", q7).data("q8", q8)
                            .data("q9", q9).data("q10", q10)
                            .data("latitude", "234").data("longitude", "344.4").data("imei", ime)
                            .data("time", MainActivity.b64)
                            .post();


                    //String title = document.title();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;

        }

我缺少的是在这里吗?我想我的数据在我发送的格式不正确。我是否需要添加的单词反馈的数据呢?
任何帮助将是AP preciated

What am i missing here? i guess the format of my data in which i am sending is incorrect. do i need to add the word feedback in the data as well? Any help would be appreciated

推荐答案

如果该服务器需要一个JSON字符串,试试这个来代替:

If the server expects a JSON string, try this instead:

// Build the JSON string...
String format = "{\"feedback\":{\"q1\":%s, \"q2\":%s, \"q3\":%s, \"q4\":%s, \"q5\":%s, \"q6\":%s, \"q7\":%s, \"q8\":%s, \"q9\":%s, \"q10\":%s, \"latitude\":%s, \"longitude\":%s, \"imei\":%s, \"time\":\"%s\"}}";
String jsonData = String.format( //
           format, //
           q1, q2, q3, q4, q5, //
           q6, q7, q8, q9, q10, //
           "234", "344.4", //
           ime, time //
);

// Now send it...
Document document = Jsoup
                    .connect("http://107.170.91.100/books/save_feedback")
                    .data("JSON", jsonData) // The parameter name may not be JSON, change accordingly
                    .post();

这篇关于使用Android的Jsoup将数据发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆