Java 如何在android中向服务器发送数据

Java 如何在android中向服务器发送数据,java,android,geolocation,Java,Android,Geolocation,我想在我的应用程序中循环发送纬度和经度。 这是使用GPS获取此参数的功能 private void showLocation(Location location) { String latitude = "Latitude: "; String longitude = "Longitude: "; if (location != null) { latitude += location.getLatitude(); longitude

我想在我的应用程序中循环发送纬度和经度。 这是使用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
    }
}
public void sendData(双纬度、双经度){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://192.x.x.x:8080/run/mypage.php");
试一试{
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“纬度”,Double.toString(纬度));
添加(新的BasicNameValuePair(“经度”,Double.toString(经度));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
我建议您使用库进行联网。你的例子可以是这样的

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();
    }
}
不要忘记在
异步任务中运行网络代码

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);
    }
}
请注意,我使用了
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”
{
 "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"
}