首页 > 学院 > 开发设计 > 正文

AsyncHttpClient

2019-11-14 23:25:28
字体:
来源:转载
供稿:网友
AsyncHttpClient Posted on 2015-03-23 10:20 鲸歌 阅读(...) 评论(...) 编辑 收藏

package com.jingle.httpstudy;

?

import org.apache.http.Header;

?

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

?

import com.loopj.android.http.AsyncHttpClient;

import com.loopj.android.http.AsyncHttPResponseHandler;

import com.loopj.android.http.JsonHttpResponseHandler;

import com.loopj.android.http.RequestParams;

?

publicclassMainActivityextendsActivity{

????privateStringURL="http://192.168.0.11:8080/NovallService/login.action";

????privateStringuserName="15995858188";

????privateStringuserPass="aa97eba124ab0c029fb7d5c37a6141b0";

????privateAsyncHttpClientclient;

?

????@Override

????protectedvoidonCreate(BundlesavedInstanceState){

????????super.onCreate(savedInstanceState);

????????setContentView(R.layout.activity_main);

????????initButtonLogin();

?

????????initButtonOffLine();

????}

?

????privatevoidinitButtonOffLine(){

????????ButtonbtnOffline=(Button)findViewById(R.id.getOffLine);

????????btnOffline.setOnClickListener(newOnClickListener(){

?

????????????@Override

????????????publicvoidonClick(Viewv){

????????????????// getOffLine();

?

????????????????getOverRun();

?

????????????}

????????});

????}

?

????privatevoidinitButtonLogin(){

????????ButtonbtnLogin=(Button)findViewById(R.id.login);

????????btnLogin.setOnClickListener(newOnClickListener(){

????????????publicvoidonClick(Viewv){

????????????????asyncLogin();

????????????}

????????});

????}

?

????privatevoidgetOverRun(){

????????Stringurl="http://192.168.0.11:8080/NovallService/getAllOverRun.action";

????????StringdeviceIds="4";

????????StringlastRequestDate="2015-03-1617:40:00";

????????RequestParamsparams=newRequestParams();

????????params.put("deviceIds",deviceIds);// 设置请求的参数名和参数值 key-value pair

????????params.put("lastRequestDate",lastRequestDate);// 设置请求的参数名和参数

//????????client.setTimeout(5000);

????????client.get(url,newJsonHttpResponseHandler(){

?

????????????publicvoidonSuccess(intstatusCode,Header[]headers,

????????????????????StringresponseString){

????????????????Log.d("ace",">>>>"+responseString+"<<<<");

????????????}

?

????????????@Override

????????????publicvoidonFailure(intstatusCode,Header[]headers,

????????????????????StringresponseString,Throwablethrowable){

????????????????Log.d("ace",">>>>"+"error"+"<<<<");

????????????????throwable.printStackTrace();

????????????}

????????});

????????// client.get(url, params, new AsyncHttpResponseHandler() {

????????//

????????// public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {

????????// Log.d("ace", new String(arg2));

????????// }

????????//

????????// public void onFailure(int arg0, Header[] arg1, byte[] arg2,

????????// Throwable arg3) {

????????// arg3.printStackTrace();

????????// }

????????// });

?

????}

?

????privatevoidgetOffLine(){

????????Stringurl="http://192.168.0.11:8080/NovallService/getAllOffLine.action";

????????StringdeviceIds="6";

????????StringlastRequestDate="2015-03-1617:40:00";

????????// 创建请求参数的封装的对象

????????RequestParamsparams=newRequestParams();

????????params.put("deviceIds",deviceIds);// 设置请求的参数名和参数值 key-value pair

????????// params.put("lastRequestDate", lastRequestDate);// 设置请求的参数名和参数

????????client.get(url,params,newAsyncHttpResponseHandler(){

?

????????????publicvoidonSuccess(intarg0,Header[]arg1,byte[]arg2){

????????????????Log.d("ace",newString(arg2));

????????????}

????????????publicvoidonFailure(intarg0,Header[]arg1,byte[]arg2,

????????????????????Throwablearg3){

????????????????arg3.printStackTrace();

????????????}

????????});

????}

?

????privatevoidasyncLogin(){

????????client=newAsyncHttpClient();

????????// 创建请求参数的封装的对象

????????RequestParamsparams=newRequestParams();

????????params.put("userName",userName);// 设置请求的参数名和参数值 key-value pair

????????params.put("passWord",userPass);// 设置请求的参数名和参数

????????client.post(URL,params,newAsyncHttpResponseHandler(){

?

????????????publicvoidonSuccess(intarg0,Header[]arg1,byte[]arg2){

????????????????if(arg0==200){

????????????????????Log.i("ace",newString(arg2));

????????????????????// tv_result.setText(new String(responseBody)); // 设置显示的文本

????????????????}

????????????}

?

????????????publicvoidonFailure(intarg0,Header[]arg1,byte[]arg2,

????????????????????Throwablearg3){

????????????????arg3.printStackTrace();

?

????????????}

????????});

????????// 查看 cookie 信息

????????// CookieSpec cookiespec = CookiePolicy.getDefaultSpec();

????????// Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/",

????????// false,

????????// client.getState().getCookies());

????????// if (cookies.length == 0) {

????????// System.out.println("None");

????????// } else {

????????// for (int i = 0; i < cookies.length; i++) {

????????// System.out.println(cookies[i].toString());

????????// }

????????// }

????}

?

}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表