码迷,mamicode.com
首页 > 移动开发 > 详细

android AsyncHttpClient使用

时间:2014-09-03 11:03:06      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   os   io   使用   strong   

1.www.github.com下载jar包

loopj/android-async-http

将下载好的文件导入项目中

2.main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"

        >
    <EditText
            android:id="@+id/et_username"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="请输入用户名"
            />
    <EditText
            android:id="@+id/et_password"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="请输入密码"
            />
    <Button
            android:onClick="click1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="asyClient get请求"
            />
    <Button
            android:onClick="click2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="asyClient post请求"
            />
</LinearLayout>

2.MyAcitivity

package com.example.aysncHttp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class MyActivity extends Activity {
    private EditText et_username;
    private EditText et_password;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        et_username = (EditText)findViewById(R.id.et_username);
        et_password = (EditText)findViewById(R.id.et_password);
    }

    public void click1(View v)
    {
        String username = et_username.getText().toString().trim();
        String password = et_password.getText().toString().trim();
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        String url = "http://172.16.1.1:91/api/test/?djxm="+username+"&password="+password;
        asyncHttpClient.get(url,new AsyncHttpResponseHandler (){
            @Override
            public void onFailure(Throwable error) {
                super.onFailure(error);

                Toast.makeText(MyActivity.this,"请求失败",1).show();
            }

            @Override
            public void onSuccess(String content) {
                super.onSuccess(content);
                Toast.makeText(MyActivity.this,content,1).show();
            }
        });

    }

    public void click2(View v)
    {
        String username = et_username.getText().toString().trim();
        String password = et_password.getText().toString().trim();
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        String url = "http://172.16.1.1:91/api/test/";
        RequestParams params = new RequestParams();
        params.put("djxm",username);
        params.put("password",password);
        asyncHttpClient.post(url,params,new AsyncHttpResponseHandler (){
            @Override
            public void onFailure(Throwable error) {
                super.onFailure(error);

                Toast.makeText(MyActivity.this,"请求失败",1).show();
            }

            @Override
            public void onSuccess(String content) {
                super.onSuccess(content);
                Toast.makeText(MyActivity.this,content,1).show();
            }
        });

    }
}

3.清单文件中配置权限

<uses-permission android:name="android.permission.INTERNET"/>

 

android AsyncHttpClient使用

标签:android   style   blog   http   color   os   io   使用   strong   

原文地址:http://www.cnblogs.com/zoro-zero/p/3953114.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!