Android AsyncHttpClient 的简单用法
2016-04-13 21:11
585 查看
直接上代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.httpapache.MainActivity" > <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Get" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Post" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scrollHorizontally="true" android:text="@string/hello_world" /> </LinearLayout>
package com.example.httpapache; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import com.loopj.android.http.RequestParams; public class MainActivity extends ActionBarActivity { private Button button,button1; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button1); textView = (TextView) findViewById(R.id.textView1); button1 = (Button) findViewById(R.id.button2); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { AsyncHttpClient client = new AsyncHttpClient(); client.get("http://www.baidu.com", new AsyncHttpResponseHandler(){ @Override public void onSuccess(int arg0, String arg1) { // TODO Auto-generated method stub super.onSuccess(arg0, arg1); textView.setText(arg1); } }); } }); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { AsyncHttpClient client = new AsyncHttpClient(); String Url = "http://www.baidu.com"; RequestParams params = new RequestParams(); client.post(Url, new AsyncHttpResponseHandler(){ @Override public void onSuccess(int arg0, String body) { // TODO Auto-generated method stub super.onSuccess(arg0, body); textView.setText(body); } }); } }); } }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories