首页 > Web开发 > 详细

AsyncHttpClient

时间:2015-03-23 11:01:41      阅读:388      评论:0      收藏:0      [点我收藏+]

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;

?

public class MainActivity extends Activity {

????private String URL = "http://192.168.0.11:8080/NovallService/login.action";

????private String userName = "15995858188";

????private String userPass = "aa97eba124ab0c029fb7d5c37a6141b0";

????private AsyncHttpClient client;

?

????@Override

????protected void onCreate(Bundle savedInstanceState) {

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

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

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

?

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

????}

?

????private void initButtonOffLine() {

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

????????btnOffline.setOnClickListener(new OnClickListener() {

?

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

????????????public void onClick(View v) {

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

?

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

?

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

????????});

????}

?

????private void initButtonLogin() {

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

????????btnLogin.setOnClickListener(new OnClickListener() {

????????????public void onClick(View v) {

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

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

????????});

????}

?

????private void getOverRun() {

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

????????String deviceIds = "4";

????????String lastRequestDate = "2015-03-1617:40:00";

????????RequestParams params = new RequestParams();

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

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

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

????????client.get(url, new JsonHttpResponseHandler() {

?

????????????public void onSuccess(int statusCode, Header[] headers,

????????????????????String responseString) {

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

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

?

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

????????????public void onFailure(int statusCode, Header[] headers,

????????????????????String responseString, Throwable throwable) {

????????????????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();

????????// }

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

?

????}

?

????private void getOffLine() {

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

????????String deviceIds = "6";

????????String lastRequestDate = "2015-03-1617:40:00";

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

????????RequestParams params = new RequestParams();

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

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

????????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();

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

????????});

????}

?

????private void asyncLogin() {

????????client = new AsyncHttpClient();

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

????????RequestParams params = new RequestParams();

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

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

????????client.post(URL, params, new AsyncHttpResponseHandler() {

?

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

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

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

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

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

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

?

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

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

????????????????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());

????????// }

????????// }

????}

?

}

AsyncHttpClient

原文:http://www.cnblogs.com/jinglecode/p/4359047.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!