标签:
1:服务器端
/**
* 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服务
*/
private void loginPostService() {
AsyncHttpServer server = new AsyncHttpServer();
server.post("/login", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest asyncHttpServerRequest, AsyncHttpServerResponse asyncHttpServerResponse) {
Headers headers = asyncHttpServerRequest.getHeaders();
/** 获取头部信息 **/
Log.v("get login headers--", headers.get("name") + ":" + headers.get("password"));
/** 返回信息 **/
asyncHttpServerResponse.send("This is post return string!");
}
});
server.listen(60000);
}
/**
* httpGet服务
*/
private void sendHttpServer(){
AsyncHttpServer server = new AsyncHttpServer();
server.get("/menu", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest asyncHttpServerRequest, AsyncHttpServerResponse asyncHttpServerResponse) {
/** 获取头部信息 **/
Headers headers = asyncHttpServerRequest.getHeaders();
Log.v("get menu headers--",headers.get("name")+":"+headers.get("password"));
/** 返回信息 **/
asyncHttpServerResponse.send("This is http return string");
}
});
server.listen(60001);
}
}
2:客户端
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连接 **/
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<header.size();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.execute(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连接 **/
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<header.size();i++){
httpGet.addHeader(header.get(i));
Log.v(header.get(i).getName(), header.get(i).getValue());
}
try {
HttpResponse response = client.execute(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;
}
}
基于android-async-http的android服务
标签:
原文地址:http://www.cnblogs.com/liu-fei/p/5182921.html