Android-async-http-based android service, android-async-http

Source: Internet
Author: User

Android-async-http-based android service, android-async-http

1: Server Side
/**
* Created by LiuFei on 2016/1/22.
*/
Public class HttpService extends Service {

@ Override
Public IBinder onBind (Intent intent ){
Return null;
}

@ Override
Public int onStartCommand (Intent intent, int flags, int startId ){
LoginPostService ();
SendHttpServer ();
Return super. onStartCommand (intent, flags, startId );
}
/**
* Http service
*/
Private void loginPostService (){
AsyncHttpServer server = new AsyncHttpServer ();
Server. post ("/login", new HttpServerRequestCallback (){
@ Override
Public void onRequest (AsyncHttpServerRequest asyncHttpServerRequest, AsyncHttpServerResponse asyncHttpServerResponse ){
Headers headers = asyncHttpServerRequest. getHeaders ();
/** Get the header information **/
Log. v ("get login headers --", headers. get ("name") + ":" + headers. get ("password "));
/** Return information **/
AsyncHttpServerResponse. send ("This is post return string! ");
}
});
Server. listen (60000 );
}

/**
* HttpGet Service
*/
Private void sendHttpServer (){
AsyncHttpServer server = new AsyncHttpServer ();
Server. get ("/menu", new HttpServerRequestCallback (){
@ Override
Public void onRequest (AsyncHttpServerRequest asyncHttpServerRequest, AsyncHttpServerResponse asyncHttpServerResponse ){
/** Get the header information **/
Headers headers = asyncHttpServerRequest. getHeaders ();
Log. v ("get menu headers --", headers. get ("name") + ":" + headers. get ("password "));
/** Return information **/
AsyncHttpServerResponse. send ("This is http return string ");
}
});
Server. listen (60001 );
}
}

2: Client
public class SetData {

private static SetData instance ;

public static SetData getInstance(){
if(instance == null){
instance = new SetData();
}
return instance ;
}

public String login(String name ,String password){
String dataIp="http://192.168.2.189:60000/login";
ArrayList<NameValuePair> listPair = new ArrayList<>();
ArrayList<Header> listHeader = new ArrayList<>();
listHeader.add(new BasicHeader("name",name));
listHeader.add(new BasicHeader("password",password));
return NetHttpData.getInstance().internet(listPair, listHeader, dataIp);
}

public String getMenu(String name ,String password){
String dataIp="http://192.168.2.189:60001/menu";
ArrayList<Header> listHeader = new ArrayList<>();
listHeader.add(new BasicHeader("name",name));
listHeader.add(new BasicHeader("password",password));
return NetHttpData.getInstance().internetGetMenu(listHeader, dataIp);
}
}
Public class NetHttpData {

Private final String ERROR = "error ";
Private static NetHttpData instance;

Public static NetHttpData getInstance (){
If (instance = null) instance = new NetHttpData ();
Return instance;
}

/** HttpPost connection **/
Public String internet (ArrayList <NameValuePair> list, ArrayList <Header> header, String dataIp ){
String result = null;
HttpPost post = new HttpPost (dataIp );
HttpClient client = new DefaultHttpClient ();
Log. I ("URL -----", dataIp );
For (int I = 0; I Post. addHeader (header. get (I ));
Log. v (header. get (I). getName (), header. get (I). getValue ());
}

For (int I = 0; I <list. size (); I ++ ){
Log. I (list. get (I). getName () + "------", list. get (I). getValue ());
}

Try {
Post. setEntity (new UrlEncodedFormEntity (list, HTTP. UTF_8 ));
HttpResponse response = client.exe cute (post );
If (response. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK ){
Result = GetString (response. getEntity ());
Log. v ("result is -----", result );
Return result;
}
} Catch (IOException e ){
E. printStackTrace ();
}
Return ERROR;
}

/** HttpGet connection **/
Public String internetGetMenu (ArrayList <Header> header, String dataIp ){
String result = null;
HttpGet httpGet = new HttpGet (dataIp );
HttpClient client = new DefaultHttpClient ();
Log. I ("URL -----", dataIp );
For (int I = 0; I HttpGet. addHeader (header. get (I ));
Log. v (header. get (I). getName (), header. get (I). getValue ());
}
Try {
HttpResponse response = client.exe cute (httpGet );
If (response. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK ){
Result = GetString (response. getEntity ());
Log. v ("result is -----", result );
Return result;
}
} Catch (IOException e ){
E. printStackTrace ();
}
Return ERROR;
}

Private String GetString (HttpEntity entity ){
Try {
InputStream input = entity. getContent ();
Int length = (int) entity. getContentLength ();
StringBuffer sb = new StringBuffer (length );
BufferedReader br = new BufferedReader (new InputStreamReader (input), length );
String line = null;
While (line = br. readLine ())! = Null ){
Sb. append (line );
}
Input. close ();
Br. close ();
Return sb. toString ();
} Catch (IllegalStateException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
Return ERROR;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.