我想在用戶關閉應用程序時清空服務器端的Mac地址表。因此,我正嘗試在ClearTable
類中調用emptyTable()
。目前,當我關閉應用程序emptyTable()
被調用,但'donInBackground()'中沒有發生什麼?是否有可能在調用onDestroy時向服務器發送請求
MainActivity類別
@Override
protected void onDestroy() {
super.onDestroy();
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String macAddress = wInfo.getMacAddress();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("mac", macAddress);
String json = jsonObject.toString();
ClearTable ct = new ClearTable();
ct.emptyTable(json);
}
ClearTable類:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import com.google.gson.Gson;
import com.json.JSONStore;
public class ClearTable {
public void emptyTable(String json){
new MyAsyncTask().execute(json);
}
class MyAsyncTask extends AsyncTask<String, Integer, ArrayList<Integer>> {
ArrayList<Integer> routes = new ArrayList<Integer>();
@Override
protected ArrayList<Integer> doInBackground(String... params) {
BufferedReader reader = null;
try {
System.out.println("The output of : doInBackground "
+ params[0]);
URL myUrl = new URL(
"https://serverside-apple.rhcloud.com/webapi/test");
HttpURLConnection conn = (HttpURLConnection) myUrl
.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.connect();
DataOutputStream wr = new DataOutputStream(
conn.getOutputStream());
// write to the output stream from the string
wr.writeBytes(params[0]);
wr.close();
System.out.println("xyz The output of getResponsecode: "
+ conn.getResponseCode());
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (reader != null) {
try {
reader.close();
return null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}
protected void onPostExecute(ArrayList<Integer> result) {
}
}
}
編輯代碼與IntentService:
在MainActivity的onDestroy:
@Override
protected void onDestroy() {
super.onDestroy();
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String macAddress = wInfo.getMacAddress();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("mac", macAddress);
System.out.println("JsonObject" + jsonObject);
String json = jsonObject.toString();
Intent intent2 = new Intent(MainActivity.this,
ClearTable.class);
intent2.putExtra("json_mac", json);
startService(intent2);
}
IntentService類:
public class ClearTable extends IntentService{
public ClearTable() {
super("IntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
BufferedReader reader = null;
try {
String jSONString = intent.getStringExtra("json_mac");
System.out.println("xyz The output of : doInBackground "
+ jSONString);
URL myUrl = new URL(
"https://serverside-apple.rhcloud.com/webapi/test");
HttpURLConnection conn = (HttpURLConnection) myUrl
.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.connect();
DataOutputStream wr = new DataOutputStream(
conn.getOutputStream());
// write to the output stream from the string
wr.writeBytes(jSONString);
wr.close();
System.out.println("xyz The output of getResponsecode: "
+ conn.getResponseCode());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
清單:
<service android:name=".ClearTable" />
在Android documntaion有關的onDestroy: 「注意:不要在這個方法不算被稱爲保存數據的地方。例如,如果一個活動是在內容提供商編輯數據,這些修改應該在致力於要麼onPause()或onSaveInstanceState(Bundle),不在這裏。「 –