欢迎使用CSDN-markdown编辑器

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

            }

            @Override
            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() {

            @Override
            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());
        // }
        // }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值