Package com.example.service;import org.apache.http.header;import org.json.jsonobject;import com.loopj.android.http.asynchttpclient;import com.loopj.android.http.jsonhttpresponsehandler;import com.loopj.android.http.RequestParams;import android.annotation.SuppressLint;import Android.app.service;import android.content.intent;import android.content.serviceconnection;import android.os.binder;import android.os.ibinder;import android.widget.toast;public class GetDateService extends Service{ /* * Request Queue */ private mybind mybind=new mybind (); @Override public void oncreate () { // TODO Auto-generated method stub super.oncreatE (); system.out.println ("Testservice oncreate ()"); } @SuppressLint ("Newapi") @Override public int onstartcommand (Intent intent, int flags, int startid) { // todo auto-generated method stub getdate ("http://www.lewei50.com/api/V1/Gateway/GetSensors/01") ; return super.onstartcommand (intent, flags, Startid); } @Override public void ondestroy () { // TODO Auto-generated Method stub &nbSp; super.ondestroy (); system.out.println ("Testservice desory ()"); } @Override public ibinder onbind (intent Intent) { // todo auto-generated method stub return myBind; } @Override public void unbindservice (serviceconnection conn) { // TODO Auto-generated method Stub system.out.println ("Test unbindservice ()"); super.unbindservice (conn); } public class mybind extends binder{ Public void test () { SYSTEM.OUT.PRINTLN ("Test"); } public getdateservice getdateservice () { return GetDateService.this; } } public void getdate (String url) { asynchttpclient client = new asynchttpclient (); requestparams params = new requestparams () ; // set parameter names and values for requests params.put ("UserKey", "cc809cd707xxxxxa917xxxxxxc674d0"); client.get (Url,params, new jsonhttpresponsehandler () { @Override public void onstart () { // called before request is started } @Override public void onsuccess (Int statuscode, header[] headers,jsonobject response) { &nbsP; // todo auto-generated method stub Super.onsuccess (Statuscode, headers, response); toast.maketext (Getdateservice.this, response.tostring (), toast.length_short). Show (); } @Override public void onfailure (int arg0, Header[] headers, byte[] arg2,         THROWABLE ARG3) { // todo auto-generated method stub super.onfailure (arg0, headers, ARG2, ARG3); toast.maketext (Getdateservice.this, arg3.tostring (), toast.length_short). Show (); } @Override public void onretry () { // TODO Auto-generated method stub super.onretry (); } }); system.out.println (URL); } }
public void getDate (String URL) {
Asynchttpclient client = new Asynchttpclient ();
Requestparams params = new Requestparams ();
Sets the parameter name and parameter of the requestNumerical
params.put ("UserKey", "cc809cd707cexxxxx9176xxxxxc674d0");
Client.get (URL,params, new Jsonhttpresponsehandler () {
@Override
public void OnStart () {
Called before request is started
}
@Override
public void onsuccess (int statusCode, header[] headers,jsonobject response) {
TODO auto-generated Method Stub
Super.onsuccess (StatusCode, headers, response);
Toast.maketext (Getdateservice.this, response.tostring (), Toast.length_short). Show ();
}
@Override
public void onfailure (int arg0, header[] headers, byte[] arg2,
Throwable arg3) {
TODO auto-generated Method Stub
Super.onfailure (arg0, headers, arg2, ARG3);
Toast.maketext (Getdateservice.this, arg3.tostring (), Toast.length_short). Show ();
}
@Override
public void Onretry () {
TODO auto-generated Method Stub
Super.onretry ();
}
});
}
The parameter format is a string type and the resulting data format is in JSON format
This article is from the "matengbing" blog, make sure to keep this source http://matengbing.blog.51cto.com/11395502/1909711
Android uses asynchttpclient to load network resources in service