如何在android中发送数据到服务器 [英] how to send data to server in android
问题描述
这是使用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);
尝试{
列表< 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);
$ b $ catch(ClientProtocolException e){
// TODO自动生成的catch块
} catch(IOException e){
// TODO自动生成的catch块
$ / code $ / pre
预先致谢4帮助
解决方案我建议您使用 OkHttp 图书馆的网络。你的例子可能是这样的
private final OkHttpClient client = new OkHttpClient();
$ b $ 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)
。建立();
响应响应= client.newCall(request).execute();
return response.body()。string();
} catch(IOException e){
returnError:+ 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
保护无效onPostExecute(字符串响应){
Log.d(networking,response);
}
}
这可以是 onCreate
您的活动方法
保护无效的onCreate(Bundle savedInstanceState){
super。的onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
位置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
}
I want to send latitude and longitude cyclically in my app.
This is the function which get this parameters using GPS
private void showLocation(Location location) {
String latitude = "Latitude: ";
String longitude = "Longitude: ";
if (location != null) {
latitude += location.getLatitude();
longitude += location.getLongitude();
}
}
I was looking methods in the web, I found some but it didn't work and it's deprecated
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
}
}
Thanks in advance 4 help
解决方案 I recommend you to use OkHttp library for networking. Your example could be something like this
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();
}
}
Don't forget to run networking code in an 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);
}
}
And this could be the onCreate
method of your activity
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);
}
Please notice that I use http://httpbin.org/post
as remote address, you must replace with your Endpoint URL. In my case the response is:
{
"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"
}
这篇关于如何在android中发送数据到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!