編輯:關於android開發
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之利用HTTP網絡通信實現與PHP的交互(三),android網絡通信 Android與PHP的交互是通過Http網絡編程來實現的,利用php訪
本節所要講的主要內容包括Android桌面小部件、App Widget的開發入
Android端 配置極光推送 因為業務需要,android客戶端需要加推送,原來采用的百度推送,但是小米手機有時候收不到,後來換成了極光推送,極光的話所有設備都能收到推
Android 手機衛士--選中SettingItemView條目狀態切換,點擊item條目跳轉頁面本文實現上篇文章中自定義組合控件中相關方法。 checkBox是否選