步骤:
1.添加权限(访问网络权限)
2.创建异步请求对象
3.执行post请求(指定url路径,新建AsyncHttpResponseHandler方法)
a) 如果成功创建工厂对象
b) 工厂对象的decodeByteArray把字节转换成Bitmap对象
c) 设置图片
实例项目解析
项目要求:通过editText文本输入框中的网址,点击查看按钮,进行网络图片显示
运行效果如下:
重点代码:均有详细解析,请认真查看注释
1、在AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.INTERNET"/>
2、布局文件activity_main.xml
package com.example.photowatch;
import org.apache.http.Header;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
public class MainActivity extends Activity {
private EditText et_url;
private ImageView img_info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取控件对象
et_url = (EditText) findViewById(R.id.et_url);
img_info = (ImageView) findViewById(R.id.img_info);
};
public void show(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_show:
//请求地址
String url = et_url.getText().toString();
//创建网络请求对象
AsyncHttpClient client= new AsyncHttpClient();
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
if (statusCode == 200) {
//创建工厂对象
BitmapFactory bitmapFactory = new BitmapFactory();
//工厂对象的decodeByteArray把字节转换成Bitmap对象
Bitmap bitmap = bitmapFactory.decodeByteArray(responseBody, 0, responseBody.length);
//设置图片
img_info.setImageBitmap(bitmap);
}
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
error.printStackTrace();
}
});
break;
default:
break;
}
}
}
3、MainActivity.java
package com.example.photowatch;
import org.apache.http.Header;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
public class MainActivity extends Activity {
private EditText et_url;
private ImageView img_info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取控件对象
et_url = (EditText) findViewById(R.id.et_url);
img_info = (ImageView) findViewById(R.id.img_info);
};
public void show(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_show:
//请求地址
String url = et_url.getText().toString();
//创建网络请求对象
AsyncHttpClient client= new AsyncHttpClient();
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
if (statusCode == 200) {
//创建工厂对象
BitmapFactory bitmapFactory = new BitmapFactory();
//工厂对象的decodeByteArray把字节转换成Bitmap对象
Bitmap bitmap = bitmapFactory.decodeByteArray(responseBody, 0, responseBody.length);
//设置图片
img_info.setImageBitmap(bitmap);
}
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
error.printStackTrace();
}
});
break;
default:
break;
}
}
}
重点代码就是这些,自己动手查看一下效果吧!~
开源框架资源:http://download.csdn.net/detail/zhaoyazhi2129/7400787
源码:http://download.csdn.net/detail/zhaoyazhi2129/7400815
转发请标明原文地址 http://blog.csdn.net/zhaoyazhi2129/article/details/27048527