Android-异步HttpClient框架(get/post)

来源:互联网 发布:超声波洗菜机 知乎 编辑:程序博客网 时间:2024/06/05 14:26

Demo 中使用了异步HttpClient框架故提供:

源码下载地址:http://download.csdn.net/detail/u014657752/9016673

效果图:



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

布局文件:

mylogin.xml

<span style="font-family:KaiTi_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/bga"    android:orientation="vertical" >    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="30dp"        android:layout_marginTop="50dp"        android:orientation="horizontal" >        <ImageView            android:id="@+id/imageView3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:contentDescription="@string/app_name"            android:src="@drawable/icon_groupmember_press" />        <EditText            android:id="@+id/username"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="20dp"            android:ems="10"            android:hint="请输入用户名" />    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="30dp"        android:layout_marginTop="50dp"        android:orientation="horizontal" >        <ImageView            android:id="@+id/imageView4"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:contentDescription="@string/app_name"            android:src="@drawable/icon_groupmember_press" />        <EditText            android:id="@+id/pwd"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="20dp"            android:ems="10"            android:hint="请输入密码"            android:inputType="textPassword" />    </LinearLayout>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:layout_marginTop="30dp"        android:orientation="vertical" >        <Button            android:id="@+id/btget"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:onClick="loginget"            android:text="get登录"            android:textSize="20sp" />         <Button            android:id="@+id/btpost"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:onClick="loginpost"            android:text="post登录"            android:textSize="20sp" />    </LinearLayout>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:layout_marginTop="30dp"        android:orientation="horizontal" >        <Button            android:id="@+id/exitButton"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:onClick="exit"            android:text="退出"            android:textSize="20sp" />    </LinearLayout></LinearLayout></span>

MainActivity.java

package com.example.android10;import java.net.URLEncoder;import org.apache.http.Header;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;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 MainActivity extends Activity {EditText et_name;EditText et_pass;Button bt_get;Button bt_post;Button bt_exit;String name;String password;String url;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.mylogin);et_name = (EditText) findViewById(R.id.username);et_pass = (EditText) findViewById(R.id.pwd);bt_get = (Button) findViewById(R.id.btget);bt_post = (Button) findViewById(R.id.btpost);bt_exit = (Button) findViewById(R.id.exitButton);}// get提交public void loginget(View v) {name = et_name.getText().toString();password = et_pass.getText().toString();url = "http://172.16.11.15:8080/nielm/servlet/nielm?userName="+ URLEncoder.encode(name) + "&password=" + password;// 创建异步httpclientAsyncHttpClient ahc = new AsyncHttpClient();// 发送get请求提交数据ahc.get(url, new MyResponseHandler());}// post提交public void loginpost(View view) {name = et_name.getText().toString();password = et_pass.getText().toString();String url = "http://172.16.11.15:8080/nielm/servlet/nielm";// 创建异步httpclientAsyncHttpClient ahc = new AsyncHttpClient();//发送post请求提交数据    //把要提交的数据封装至RequestParams对象RequestParams params = new RequestParams();params.add("userName", name);params.add("password", password);ahc.post(url, params, new MyResponseHandler());}class MyResponseHandler extends AsyncHttpResponseHandler {@Overridepublic void onSuccess(int statusCode, Header[] headers,byte[] responseBody) {String string = new String(responseBody);if (string.equals("1")) {Toast.makeText(MainActivity.this, "登录成功!!", Toast.LENGTH_SHORT).show();} else {Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();}}@Overridepublic void onFailure(int statusCode, Header[] headers,byte[] responseBody, Throwable error) {Toast.makeText(MainActivity.this, "连接失败", Toast.LENGTH_SHORT).show();}}public void exit(View view) {finish();Toast.makeText(MainActivity.this, "退出成功!!", Toast.LENGTH_SHORT).show();}}





0 0
原创粉丝点击