如何从安卓系统向服务器发送数据[英] how to send data from android to server

本文是小编为大家收集整理的关于如何从安卓系统向服务器发送数据的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在探索Android的扫描SSID和RSSI,我可以做到这一点,但是现在我想将数据发送到服务器,所以我探索了我在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数据时遇到问题,格式应如下

http://101.34.45.45/rawdata?data?data?data?data?data? Guest1"," Timestamp":" 2010-07-01 08:58:23"," WiFi":[{" ssid":" guest"," rssi":" 40"},{guest {guest'ssid":":"" "," rssi":":" 80"}]},

因此,如何为WiFi参数做,任何人都可以提供帮助,我仍在尝试一些品种,

谢谢

推荐答案

我建议您切换到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的类似问题的示例代码.

this 也可能有用.

本文地址:https://www.itbaoku.cn/post/102492.html

问题描述

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

推荐答案

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.