【发布时间】:2015-10-29 07:20:12
【问题描述】:
我正在尝试通过意图服务中的 POST 方法发送数据。但是如果数据连接(互联网)不好,应用程序就会崩溃。有人建议我使用线程而不是意图服务,因为线程可以在通过 POST 方法发送数据时处理多个实例,但意图服务不能。请建议我是否应该完全使用线程而不是 IntentService 或尝试 IntentService 中的 POST 方法的线程。提前致谢。附言我有一个 timertask 在我的 MainActivity 中启动 IntentService 以每 2 分钟启动一次服务。
package com.example.anew;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.ParseException;
public class LocalService extends IntentService{
String Sender, TimeStamp, Mesg, webResponse, value;
SQLiteDatabase db;
public static final String PREFS_NAME = "MyApp_Settings";
public LocalService() {
super("LocalService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
Cursor c=db.rawQuery("SELECT * FROM student", null);
if(c.getCount()==0){
System.out.println("No SMS found");
return;
}
else if(c.moveToFirst()){
for(int i=0;i<c.getCount();i++){
Sender = c.getString(1);
TimeStamp = c.getString(2);
Mesg = c.getString(3);
readWebPage(c.getString(0)); //POST method
c.moveToNext();
}
}
c.close();
}
private void readWebPage(String id) {
// TODO Auto-generated method stub
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
value = settings.getString("code", "");
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://example.com/mail.asmx/send?");
//Post Data
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(5);
nameValuePair.add(new BasicNameValuePair("id", value));
nameValuePair.add(new BasicNameValuePair("type", "IN"));
nameValuePair.add(new BasicNameValuePair("source", Sender));
nameValuePair.add(new BasicNameValuePair("time", TimeStamp));
nameValuePair.add(new BasicNameValuePair("body", Mesg));
//Encoding POST data
try{
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
System.out.println(e);
}
//making POST request.
try{
HttpResponse response = httpClient.execute(httpPost);
String XmlString = EntityUtils.toString(response.getEntity());
XmlString=XmlString.replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim();
XmlString = XmlString.substring(XmlString.indexOf("[") + 1, XmlString.lastIndexOf("]"));
JSONObject jObj = new JSONObject(XmlString);
webResponse = jObj.getString("status");
System.out.println("Web Response:" + webResponse);
if(webResponse.equals("ok")){
if(id!=null){
db.execSQL("DELETE FROM student WHERE id="+id);
}
else{
System.out.println("No SMS found");
}
}
}catch (ClientProtocolException e) {
// Log exception
e.printStackTrace();
System.out.println(e);
} catch (IOException e) {
// Log exception
e.printStackTrace();
System.out.println(e);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // readWebPage ends
}
【问题讨论】:
标签: android http-post android-intentservice