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
- Android-异步HttpClient框架(get/post)
- Android HttpClient POST GET
- Android-使用HttpClient框架提交数据(get/post)
- Android中使用HttpClient框架进行get/post提交
- android httpclient get post 代码
- Android中的异步HttpClient框架
- java/android httpclient post/get用法
- Android HttpClient基本使用方法!!--get和post
- Android HttpClient get、post 访问网络
- android:HttpClient请求(get、post)
- Android——HttpClient(get&post)
- Xarmin HttpClient通信Get/POst请求,异步处理!
- HttpUrlConnection和HttpClient和android-async-http框架的GET和POST请求
- get、post、httpclient-get、httpclient-post
- httpClient get post 实例
- HttpClient get, post使用
- HttpClient Get和Post
- httpClient post/get请求。
- HDOJ 2544 最短路(最短路径 dijkstra算法,SPFA邻接表实现,floyd算法)
- Ansible
- linux(七)
- 1079. Total Sales of Supply Chain (25)
- QObject 出现undefined vtable的解决方法
- Android-异步HttpClient框架(get/post)
- POJ 3648 Wedding(2-SAT 拓扑排序输出任意一种解决方案)
- 1084. Broken Keyboard (20)
- C Runtime Library 与 STL
- linux(八)
- hive学习笔记-表操作
- UVA 10870 Recurrences 矩阵乘法
- android adb常用指令
- [dfs]POJ3050 Hopscotch