我在休息2 - 3年后回到Android编程。以前我会使用HttpClient
来执行以下操作:
List<NameValuePair> nvp = new ArrayList<NameValuePair>(1);
nvp.add(new BasicNameValuePair("User_ID", userID));
nvp.add(new BasicNameValuePair("Latitude", lat));
nvp.add(new BasicNameValuePair("Longitude", lon));
nvp.add(new BasicNameValuePair("GPS_Accuracy", acc));
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://url/add_post_to_table.php");
httpPost.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(ClientProtocolException e)
{
...
}
catch(IOException e)
{
...
}
此方法将设置为AlarmManager和BroadcastReciever定期更新,POST将由Php脚本处理,该脚本将其存储在SQL表中。
回到Android之后,似乎有太多选项来替换此功能。 要明确的是,我要替换的功能是:定期使用用户GPS数据更新SQL表。有很多方法可以做到这一点,但是发送帖子的所有方法看起来都过于复杂在位置变化。
似乎有4个热门选项
httpURLconnection的异步任务。
Retrofithttp:square.github.io/retrofit /
Volleyhttps:developer.android.com/training/volley/index.html
Robospice:github.com/stephanenicolas/robospice/wiki/Starter-Guide
对不起的链接感到抱歉,github限制了您可以发布的链接数量&lt; 10分
也许它只是我,但#1似乎是最简单的。然而,互联网上的大多数人(包括stackoverflow)似乎都在发誓#2-4。我遇到的问题是这些解决方案(特别是#2-4)中的每一个都相当深入,无法学习并提供比我想学的更多的功能。话虽如此, 2017年7月用用户GPS数据更新远程SQL表的最简单方法是什么?
答案 0 :(得分:0)
你发现Volley / Retrofit很复杂,所以这里有你可以关注的AsyncTask样本
private class AsyncLocationUpload extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
//DO something
}
@Override
protected String doInBackground(String... params) {
String stgUrl = params[0];
String data = params[1];
try {
URL url = new URL(stgUrl);
HttpURLConnection urlconnection = (HttpURLConnection) url.openConnection();
urlconnection.setDoInput(true);
urlconnection.setDoOutput(true);
urlconnection.setRequestMethod("POST");
urlconnection.setUseCaches(false);
urlconnection.setConnectTimeout(30000);
urlconnection.setReadTimeout(30000);
urlconnection.setRequestProperty("Content-Type", "application/json");
urlconnection.setRequestProperty("Accept", "application/json");
OutputStreamWriter out = new OutputStreamWriter(urlconnection.getOutputStream());
out.write(data);
out.close();
responseCode = urlconnection.getResponseCode(); //check if your request was successful
if (responseCode != 200) {
//Failed
} else {
//Success
InputStream inputStream = urlconnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String resultString;
while ((resultString = bufferedReader.readLine()) != null) {
resultBuffer.append(resultString);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return resultBuffer.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//Do Something
}
}
创建要发送的参数的JsonObject
JSONObject locationParameter = new JSONObject();
locationParameter.put("User_ID", userID);
locationParameter.put("Latitude", lat);
locationParameter.put("Longitude", lon);
locationParameter.put("GPS_Accuracy", acc);
String url="";
new AsyncLocationUpload(this).execute(url,String.valueOf(locationParameter));
答案 1 :(得分:0)
谢谢Subhechhu,
对于任何可能在未来遇到这种情况的人来说,这就是我最终要做的事情:
import com.loopj.android.http.*;
import cz.msebera.android.httpclient.Header;
public void asyncPost() {
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("user_ID", user_ID);
params.put("gps_acquire_time", gpsAquireTime);
params.put("lat", lat);
params.put("lon", lon);
params.put("acc", acc);
client.post("http://website.com/phpscript.php", params, new TextHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, String res) {
Log.e(TAG, res);
}
@Override
public void onFailure(int statusCode, Header[] headers, String res, Throwable t) {
Log.e(TAG, res);
}
}
);
}
它使用Loopj AsyncHttpClient库,使整个过程非常简单。然后将其包含在服务中,以便它可以在后台和应用程序关闭后继续发送更新。