我想每隔60秒定期使用HttpURLConnection api将JSON字符串发布到localhost服务器(WAMP),以将其插入数据库中。因此,我正在从计时器方法执行MyAsyncTask。在AlarmManager和Service的帮助下实现该方法是更好的方法,还是足以满足我的目的?
感谢您的帮助。
PostData类:
package com.bustracker;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.AsyncTask;
import android.os.Handler;
public class PostData {
String jSONString;
Handler handler = new Handler();
public PostData(String jSONString) {
super();
this.jSONString = jSONString;
}
public String getjSONString() {
return jSONString;
}
public void setjSONString(String jSONString) {
this.jSONString = jSONString;
}
public void timer() {
new Thread(new Runnable() {
@Override
public void run() {
boolean run = true;
while (run) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
new MyAsyncTask().execute(jSONString);
}
}, 5000);
}
}
}).start();
}
class MyAsyncTask extends AsyncTask<String, Integer, Void> {
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
try {
//This is the ip address of my laptop wifi because I am running the app in my device and I want to send the data to the localhost server(WAMP).
URL myUrl = new URL("http://192.168.127.56/webservice");
HttpURLConnection myConnection = (HttpURLConnection) myUrl
.openConnection();
myConnection.setRequestMethod("POST");
myConnection.setDoOutput(true);
myConnection.setUseCaches(false);
myConnection.setConnectTimeout(10000);
myConnection.setReadTimeout(10000);
myConnection.setRequestProperty("Content-Type",
"application/json");
myConnection.connect();
// create data output stream
DataOutputStream wr = new DataOutputStream(
myConnection.getOutputStream());
// write to the output stream from the string
wr.writeBytes(jSONString);
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
传递给PostData类的JSON字符串:
{
"latitude":41.86907321,
"longitude":16.66542435,
"formatted":"22.04.2015 11:11:00",
"route":4
}
编辑:
该代码在MainActivity的内部类“ MyLocationListern”的onChanedLocation中被调用:
String jSONString = convertToJSON(pLong, pLat, formatted);
PostData sender = new PostData(jSONString);
Intent intent3 = new Intent(MainActivity.this, PostData.class);
PendingIntent pintent3 = PendingIntent.getService(getApplicationContext(), 0, intent3, 0);
AlarmManager alarm3 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
// for 30 mint 60*60*1000
alarm3.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
1000, pintent3);
startService(new Intent(getBaseContext(), PostData.class));
具有IntentService的新PostData类:
package com.bustracker;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.IntentService;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
public class PostData extends IntentService {
String jSONString;
Handler handler = new Handler();
public PostData(String jSONString) {
super("some");
this.jSONString = jSONString;
}
public String getjSONString() {
return jSONString;
}
public void setjSONString(String jSONString) {
this.jSONString = jSONString;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new MyAsyncTask().execute(jSONString);
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
}
class MyAsyncTask extends AsyncTask<String, Integer, Void> {
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
try {
//This is the ip address of my laptop wifi because I am running the app in my device and I want to send the data to the localhost server(WAMP).
URL myUrl = new URL("http://192.168.x.x/webservice");
HttpURLConnection myConnection = (HttpURLConnection) myUrl
.openConnection();
myConnection.setRequestMethod("POST");
myConnection.setDoOutput(true);
myConnection.setUseCaches(false);
myConnection.setConnectTimeout(10000);
myConnection.setReadTimeout(10000);
myConnection.setRequestProperty("Content-Type",
"application/json");
myConnection.connect();
// create data output stream
DataOutputStream wr = new DataOutputStream(
myConnection.getOutputStream());
// write to the output stream from the string
wr.writeBytes(jSONString);
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
最佳答案
首先,我们创建这样的服务
public class ChatSevice extends IntentService{
public ChatSevice() {
super("Some");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
//this is asynk task class
new ChatConnect(ChatSevice.this).execute();
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
}
}
现在以这种方式调用此服务
Intent intent3 = new Intent(this, ChatSevice.class);
PendingIntent pintent3 = PendingIntent.getService(this, 0, intent3, 0);
AlarmManager alarm3 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// for 30 mint 60*60*1000
alarm3.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
1000, pintent3);
startService(new Intent(getBaseContext(), ChatSevice.class));