[英]android http post as a service
can any one please tell me how to make this code work in the background and what is the difference when i make the code work in the background as in "do in background" and a service and which approach should i take 任何人都可以请告诉我如何让这个代码在后台工作,当我使代码在后台工作时有什么不同,如“在后台做”和服务,我应采取哪种方法
thanks to all in advance 感谢所有提前
this is the code: 这是代码:
public void SticketFunction(double book, double libadd, long time){
Log.v("log_tag", "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SticketFunction()");
//HttpClient
HttpClient nnSticket = new DefaultHttpClient();
//Response handler
ResponseHandler<String> res = new BasicResponseHandler();
HttpPost postMethod = new HttpPost("http://www.books-something.com");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("book", book+""));
nameValuePairs.add(new BasicNameValuePair("libAss", libass+""));
nameValuePairs.add(new BasicNameValuePair("Time", time+""));
nameValuePairs.add(new BasicNameValuePair("name", "jack"));
//Encode and set entity
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
//Execute
//manSticket.execute(postMethod);
String response =Sticket.execute(postMethod, res).replaceAll("<(.|\n)*?>","");
if (response.equals("Done")){
//Log.v("log_tag", "!!!!!!!!!!!!!!!!!! SticketFunction got a DONE!");
}
else Log.v("log_tag", "!!!!!!!?????????? SticketFunction Bad or no response: " + response);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
//Log.v("log_tag", "???????????????????? SticketFunction Client Exception");
} catch (IOException e) {
// TODO Auto-generated catch block
//Log.v("log_tag", "???????????????????? IO Exception");
}
}
}
You do not need a service for this. 您不需要为此服务。 I believe you could use the AsyncTask if you don't want to block your application while doing the HTTP request.
如果您不想在执行HTTP请求时阻止应用程序,我相信您可以使用AsyncTask。 It provides a handy interface for showing results on the ui.
它提供了一个方便的界面,用于显示ui上的结果。
Another option is using the Handler class. 另一种选择是使用Handler类。
The benefit of running a task in a Service
is that it is not destroyed if the user backs out of the calling Activity
. 在
Service
中运行任务的好处是,如果用户退出调用Activity
,则不会销毁它。
You could look into extending IntentService and implementing onHandleIntent , which automatically does work on a separate thread. 您可以考虑扩展IntentService并实现onHandleIntent ,它可以自动在单独的线程上运行。
When using a Service
(or IntentService
)in this situation, you would pass the NameValuePair
values in a Bundle . 在这种情况下使用
Service
(或IntentService
)时,您将在Bundle中传递NameValuePair
值。 You would also need to save the Response
data to persistent storage (database, preferences, etc), for later retrieval in an Activity
. 您还需要将
Response
数据保存到持久存储(数据库,首选项等),以便以后在Activity
进行检索。
Alternatively, you can run an AsyncTask in a regular Service
(implementing onStartCommand
), or in the calling Activity
. 或者,您可以在常规
Service
(实现onStartCommand
)或调用Activity
运行AsyncTask 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.