经过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将由存储在SQL表中的Php处理。
回到Android之后,替换这个功能的选项似乎太多了。需要明确的是,我试图替换的功能是:定期使用用户GPS数据更新SQL表。有很多方法可以做到这一点,但所有这些方法似乎都过于复杂,无法在位置更改时发送帖子。
似乎有四种流行的选择
很抱歉出现坏链接,github限制了您可以用<10分发布的链接的数量
也许只有我一个人,但#1似乎是最简单的。然而,互联网上的大多数人(包括堆叠溢出)似乎都对#2-4发誓。我遇到的问题是,每个解决方案(尤其是#2-4)都非常需要学习,而且提供的功能比我想学的要多得多。话虽如此,在2017年7月用用户GPS数据更新远程SQL表的最简单的方法是什么?
发布于 2017-07-12 06:01:22
您发现了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));发布于 2017-07-13 03:28:25
谢谢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库,使整个过程非常简单。然后将它封装到一个服务中,这样它就可以在后台和应用程序关闭后继续发送更新。
https://stackoverflow.com/questions/45044254
复制相似问题