zoukankan      html  css  js  c++  java
  • 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());

            // }

            // }

        }

     

    }

  • 相关阅读:
    oracle_jdbc_insert_into
    MySQL 性能比较测试:MySQL 5.6 GA -vs- MySQL 5.5
    centos去下载mysql应该怎么选择linux版本
    centos_radhat升级系统
    phpmyadmin 配置方法
    js 判断js函数,变量是否存在
    mysqlbinlog- 处理二进制日志文件的实用工具 学习笔记
    linux基本命令
    4 MySQL程序概述(包含mysql配置文件配置原理)-学习笔记
    Python学习笔记4 高级特性_20170618
  • 原文地址:https://www.cnblogs.com/jinglecode/p/4359047.html
Copyright © 2011-2022 走看看