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

		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() {

			@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 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() {
			//开始请求
			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 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() {

			@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 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;
    }
}




你可能感兴趣的:(xUtils中的HttpUtils请求网络数据和BitmapUtils加载网络图片)