xUtils中的HttpUtils请求网络数据和BitmapUtils加载网络图片

<span style="white-space:pre">		</span>http=new HttpUtils();
		
		// 设置请求参数的编码
		RequestParams params=new RequestParams();	//默认编码UTF-8
		params.addBodyParameter("act", "index");
		params.addBodyParameter("op", "index");
		//发送请求
		http.send(HttpRequest.HttpMethod.POST, "http://www.iginwa.com/mobile/index.php", params, new RequestCallBack<String>() {

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
			}
			
			@Override
			public void onLoading(long total, long current, boolean isUploading) {
				// TODO Auto-generated method stub
				super.onLoading(total, current, isUploading);
			}
			
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				// TODO Auto-generated method stub
				
			}
		});


上面的是UttpUtils的get和post请求网络数据

下面试BitmapUtils加载网络图片:

package com.example.xutils_test;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.example.bitmaphelper.BitmapHelp;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

public class MainActivity extends Activity 
{
	//定义成员变量
	private ImageView iv;
	private Button btn;
	
	private BitmapUtils bitmapUtils;
	private PopupWindow popup;		//PopupWindow对象
	
	private HttpUtils http;		//网络请求HttpUtils对象
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		init();
	}
	
	public void init()
	{
		//找对象
		iv=(ImageView) super.findViewById(R.id.iv);
		btn=(Button) super.findViewById(R.id.btn);
		
		//按钮加监听
		btn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
//				bitampTest();
				httpUtilsGet();
			}
		});
		
	}
	
	//练习BitmapUtils加载图片
	public void bitampTest()
	{
		bitmapUtils=BitmapHelp.getBitmapUtils(this.getApplicationContext());	//创建BitmapUtils对象,通过xUtils框架获取
		bitmapUtils.configDefaultLoadingImage(R.drawable.ic_launcher);	//设置默认图片
		bitmapUtils.configDefaultLoadFailedImage(R.drawable.bitmap);	//设置加载失败的默认图片
		bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);	//设置图片清晰度
		
		bitmapUtils.display(iv, "http://pic39.nipic.com/20140327/18204781_171251136126_2.jpg");	//加载图片     参数:1.ImageView 2.url
	}
	
	//练习HttpUtils用get方法请求网络数据
	public void httpUtilsGet()
	{
		http=new HttpUtils();
		http.send(HttpRequest.HttpMethod.GET, "http://www.iginwa.com/mobile/index.php?act=index&op=index", new RequestCallBack<String>() {
			//开始请求
			public void onStart() {
				super.onStart();
				Log.i("TAG", "用get方法开始请求数据");
			}
			
			//请求中
			public void onLoading(long total, long current, boolean isUploading) {
				// TODO Auto-generated method stub
				super.onLoading(total, current, isUploading);
			}
			
			//请求成功
			public void onSuccess(ResponseInfo<String> responseInfo) {
				Log.i("TAG", "获取的数据是:"+responseInfo.result);
			}

			//请求失败
			public void onFailure(HttpException error, String msg) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	//练习HttpUtils用post方法请求网络数据
	public void httpUtilsPost()
	{
		http=new HttpUtils();
		
		// 设置请求参数的编码
		RequestParams params=new RequestParams();	//默认编码UTF-8
		params.addBodyParameter("act", "index");
		params.addBodyParameter("op", "index");
		//发送请求
		http.send(HttpRequest.HttpMethod.POST, "http://www.iginwa.com/mobile/index.php", params, new RequestCallBack<String>() {

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
			}
			
			@Override
			public void onLoading(long total, long current, boolean isUploading) {
				// TODO Auto-generated method stub
				super.onLoading(total, current, isUploading);
			}
			
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

这是BitmapHelper帮助类,用于创建BitmapUtils对象

import android.content.Context;
import com.lidroid.xutils.BitmapUtils;

public class BitmapHelp {
    private BitmapHelp() {
    }

    private static BitmapUtils bitmapUtils;

    public static BitmapUtils getBitmapUtils(Context appContext) {
        if (bitmapUtils == null) {
            bitmapUtils = new BitmapUtils(appContext);
        }
        return bitmapUtils;
    }
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值