如何从android发送数据到服务器

发布于 2024-09-08 04:10:59 字数 1079 浏览 3 评论 0原文

我正在探索从 android 扫描 ssid 和 rssi,我能够做到这一点,但现在我想将数据发送到服务器,所以我探索我发现了下面的代码,

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://101.34.45.45/rawData");

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("userId", "00-22-68-E8-EC-F1"));
        nameValuePairs.add(new BasicNameValuePair("timestamp", "2010-07-01 11:11:11"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);
        textView.setText(response.toString());

    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }

但现在我在添加多个 wifi 时遇到问题数据,格式应如下

http://101.34.45.45/rawData?data={" userId":"guest1","timestamp":"2010-07-01 08:58:23","wifi":[{"ssid":"guest","rssi":"40"},{"ssid ":"guest1","rssi":"80"}]},

那么wifi参数怎么办,谁能帮忙,我还在尝试几个品种,

谢谢

I'm exploring to scan ssid and rssi from android and I'm able to do it, but now i have want send the data to the server, so i explore i found below code

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://101.34.45.45/rawData");

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("userId", "00-22-68-E8-EC-F1"));
        nameValuePairs.add(new BasicNameValuePair("timestamp", "2010-07-01 11:11:11"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);
        textView.setText(response.toString());

    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }

but now i'm having problem when add multi wifi data, the format should as below

http://101.34.45.45/rawData?data={"userId":"guest1","timestamp":"2010-07-01 08:58:23","wifi":[{"ssid":"guest","rssi":"40"},{"ssid":"guest1","rssi":"80"}]},

so how can done for the wifi parameter, can anyone help, I'm still trying for few varieties,

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一世旳自豪 2024-09-15 04:10:59

我建议您切换到 JSON 以将数据发送到服务器。 Google gson 最容易用来发送和解析 JSON。

{"userId":"guest1","timestamp":"2010-07-01 08:58:23","wifi":[{"ssid":"guest","rssi":"40"},{"ssid":"guest1","rssi":"80"}]}

您应该使用具有 JSON 数组作为其项目之一的 JSON 对象。 JSON 数组又包含另一个 json 对象。

如果您使用 Google GSON,则应该为其构建一个类层次结构。您应该这样做。

Class data{

 String userId;
 String timestamp;
 Wifi wifi;
}

Class Wifi{

 String ssid;
 int rssi;
}

您可以在此处查看关于在 android 中解析 json 的类似问题的示例代码。

可能也很有用。

I would recommend you to switch to JSON for sending data to the server. Google gson is easiest to use to send and parse JSON.

{"userId":"guest1","timestamp":"2010-07-01 08:58:23","wifi":[{"ssid":"guest","rssi":"40"},{"ssid":"guest1","rssi":"80"}]}

You should use a JSON object having a JSON array as one of its items. The JSON array in turn contains another json object.

If you are using Google GSON, you should build a class hierarchy for the same. Heres how you should do the same.

Class data{

 String userId;
 String timestamp;
 Wifi wifi;
}

Class Wifi{

 String ssid;
 int rssi;
}

You can check here for a sample code on a similar problem on parsing json in android.

This may be useful too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文