【问题标题】:how to send data to server in android [duplicate]如何在android中将数据发送到服务器[重复]
【发布时间】:2016-02-10 19:10:55
【问题描述】:

我想在我的应用程序中循环发送纬度和经度。 这是使用 GPS 获取此参数的函数

 private void showLocation(Location location) {
    String latitude = "Latitude: ";
    String longitude = "Longitude: ";

    if (location != null) {
        latitude += location.getLatitude();
        longitude += location.getLongitude();
}
}

我在网上寻找方法,我找到了一些方法,但没有用,已弃用

public void sendData(double latitude, double longitude){
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.x.x.x:8080/run/mypage.php");
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

        nameValuePairs.add(new BasicNameValuePair("Latitude", Double.toString(latitude)));
        nameValuePairs.add(new BasicNameValuePair("Longitude", Double.toString(longitude)));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

【问题讨论】:

  • 获取位置或将其发布到服务器有问题吗?
  • 发布到服务器

标签: java android geolocation


【解决方案1】:

我建议您使用OkHttp 库进行网络连接。你的例子可能是这样的

private final OkHttpClient client = new OkHttpClient(); 

public String sendData(double latitude, double longitude){
    try {
        RequestBody formBody = new FormBody.Builder()
                .add("Latitude", Double.toString(latitude))
                .add("Longitude", Double.toString(longitude))
                .build();

        Request request = new Request.Builder()
                .url("http://httpbin.org/post")
                .post(formBody)
                .build();

        Response response = client.newCall(request).execute();
        return response.body().string();
    } catch (IOException e) {
        return "Error: " + e.getMessage();
    }
}

别忘了在AsyncTask 中运行网络代码

class IOAsyncTask extends AsyncTask<Location, Void, String> {

    @Override
    protected String doInBackground(Location... params) {
        return sendData(params[0].getLatitude(), params[0].getLongitude());
    }

    @Override
    protected void onPostExecute(String response) {
        Log.d("networking", response);
    }
}

这可能是您活动的onCreate 方法

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Location current = new Location("");
    current.setLatitude(23.9569596);
    current.setLongitude(12.567567);

    new IOAsyncTask().execute(current);
}

请注意,我使用http://httpbin.org/post 作为远程地址,您必须替换为您的端点 URL。在我的情况下,响应是:

{
 "args": {}, 
 "data": "", 
 "files": {}, 
 "form": {
   "Latitude": "23.9569596", 
   "Longitude": "12.567567"
 }, 
 "headers": {
   "Accept-Encoding": "gzip", 
   "Content-Length": "39", 
   "Content-Type": "application/x-www-form-urlencoded", 
   "Host": "httpbin.org", 
   "User-Agent": "okhttp/3.0.1"
 }, 
 "json": null, 
 "origin": "xxx.xx.xxx.xx", 
 "url": "http://httpbin.org/post"
}

【讨论】:

  • 嗨,感谢您的帮助,但我在 RequestBody formBody = new FormBody.Builder() android studio display 中遇到了另一个问题,无法解析方法 'FormBody'
猜你喜欢
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 2013-10-27
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 2017-07-12
  • 2014-07-10
相关资源
最近更新 更多