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

            // }

            // }

        }

     

    }

  • 相关阅读:
    Centos(Linux)安装openoffice教程
    IDEA快速创建一个简单的SpringBoot项目(需要联网)
    Maven配置使用阿里云镜像
    关于Spring MVC的问题
    FTP搭建YUM源服务器
    IP子网的划分
    交换分区swap
    sentos7忘记root密码,重置密码
    sentos中bonding(网卡绑定技术)1
    sentos7网卡改名
  • 原文地址:https://www.cnblogs.com/jinglecode/p/4359047.html
Copyright © 2020-2023  润新知