如何从Android的数据发送到服务器

如何从Android的数据发送到服务器

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

问题描述

我探索从机器人扫描SSID和RSSI,我能够做到这一点,但现在我有希望将数据发送到服务器,所以我探讨,我发现低于code

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) {
    }

但现在我遇到的问题,当添加多的WiFi数据,格式应如下

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

http://101.34.45.45/rawData?data= {用户id:guest1虚拟机,时间戳:2010-07-01 8时58分23秒,无线上网:[{SSID:客,RSSI:40},{SSID:guest1虚拟机 RSSI:80}]}

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参数做了,谁能帮助,我还在努力的少数品种,

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

感谢

推荐答案

我会建议您切换到JSON用于将数据发送到服务器。 谷歌GSON 是最容易使用的发送和解析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"}]}

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

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

如果您使用的是谷歌GSON,你应该建立在相同的类层次结构。继承人你应该怎么做。

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;
}

您可以检查这里对类似的问题的样本code在Android的解析JSON。

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

可能是有用的了。

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

08-04 04:31