如何从android发送数据到服务器
我正在探索从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您切换到 JSON 以将数据发送到服务器。 Google gson 最容易用来发送和解析 JSON。
您应该使用具有 JSON 数组作为其项目之一的 JSON 对象。 JSON 数组又包含另一个 json 对象。
如果您使用 Google GSON,则应该为其构建一个类层次结构。您应该这样做。
您可以在此处查看关于在 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.
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.
You can check here for a sample code on a similar problem on parsing json in android.
This may be useful too.