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的更多相关文章

  1. 异步网络加载开源框架AsyncHttpClient使用

    AsyncHttpClient是异步的,但是有时候我们需要得到请求的结果集来返回给某个函数,由于是异步的,所以不能够直接return会去,所以可以定义一个interface来给调用AsyncHttpC ...

  2. AsyncHttpClient 源码分析

    上一篇文章从功能和用法上对AsyncHttpClient做了个大致介绍,今天我们和以往一样,从内部实现.原理的角度带领大家看看 其工作机制,以便加深理解.写程序越多,我发现每接触一个新东西,都会有强烈 ...

  3. android-async-http AsyncHttpClient介绍

    前一阵子刚搬了家,加上公司要发新版本,所以一直比较忙,文章也好几周没更新了.难得这个周末有空,正好周内偶然间闲逛 发现这个很不错的第三方开源类库,针对Android开发中发送http请求的. 在And ...

  4. FastJson和AsyncHttpCLient

    Android的展示数据,除了上章所讲的本地存储外,大部分数据都来自于网络.首先介绍一下Android APP开发常见的网络操作方式.从网络层面上有底层的tcp/ip,也就是我们常见的socket套接 ...

  5. Android应用中使用AsyncHttpClient来异步网络数据(转载)

    摘要: 首先下载AsyncHttpClient的库文件,可以自行搜索,可以到下面地址下载 http://download.csdn.net/detail/xujinyang1234/5767419 测 ...

  6. asynchttpClient框架关于多文件批量上传的问题,改用xUtil

    RequestParams params = new RequestParams(); params.add("ordernum",ordernum); params.add(&q ...

  7. Asynchttpclient开源框架下载图片和文本,于Volley和Glide开源框架的区别。

    AsyncHttpClient是一款比较流行的Android异步网路加载库,在github上的网址是:https://github.com/loopj/android-async-httpAsyncH ...

  8. android AsyncHttpClient 开源框架的使用

    AsyncHttpClient 1.在很多时候android都需要进行网络的操作,而android自带的HttpClient可以实现,但要进行很多网络连接的时候(如:下载很多图片),就需要线程池来进行 ...

  9. 强大的网络通信框架(不实现缓存)--第三方开源--AsyncHttpClient

    AsyncHttpClient是一款比较流行的Android异步网路加载库,在github上的网址是:https://github.com/loopj/android-async-http但是Asyn ...

随机推荐

  1. 嵌入式实时操作系统μCOS原理与实践任务控制与时间的解析

    /*************************************************************************************************** ...

  2. yii框架常用url地址

    调用YII框架中 jquery:Yii::app()->clientScript->registerCoreScript('jquery');        framework/web/j ...

  3. json中loads的用法

    python中json中的loads()和dumps()它们的作用经常弄换了,这里记录下,loads方法是把json对象转化为python对象,dumps方法是把pyhon对象转化为json对象,我是 ...

  4. python之字符聊天小工具

    server side: # coding: gb2312#socket server端#获取socket构造及常量from socket import *#''代表服务器为localhostmyHo ...

  5. String与Date、Timestamp互转

    一.String与Date(java.util.Date)互转 1.1 String -> Date String dateStr = "2010/05/04 12:34:23&quo ...

  6. Ajax技术详解

    Ajax技术:Ajax描述了一种主要使用脚本(JS)操纵HTTP的web应用架构,它的主要特点是使用脚本操纵HTTP和web服务器进行数据交换,不会导致页面重载.Ajax的核心是JS的XMLHttpR ...

  7. Entity Framework ModelFirst尝试

    前言 Model First我们称之为“模型优先”,这里的模型指的是“ADO.NET Entity Framework Data Model”,此时你的应用并没有设计相关数据库,在Visual Stu ...

  8. sql批量获取wordpress所有留言者的邮件地址

    如果你的wordpress博客有很多读者互动的话,他们的留言都会留下具体的联系邮箱,我们如何批量导出这些联系信息呢?可以试试下面的sql语句 SELECT DISTINCT comment_autho ...

  9. Brackets(区间dp)

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3624   Accepted: 1879 Descript ...

  10. NGUI的部分控件无法更改layer?

    http://momowing.diandian.com/post/2012-09-17/40038835795 今天狗日的遇到这样的问题,这是一个imagebutton:,它的层定义为:,NGUI里 ...