AsyncHttpClient

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

????????// }

????????// }

????}

?

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。