Volley初次尝试

一、引言

坚持就是胜利,每天进步一点点,每天快乐一点点

二、Volley是什么?

看这里:Volley框架简介

三、Volley怎么用?

第一步:导入jar包或者源码,我这里选择导入源码工程,毕竟是学习嘛

如图:

第二步:将其导入到workspace之后,新建一个项目,名为VolleyTest,如下图:

第三步 添加依耐关系

右键VolleyTest项目->properites这里相信大家都会


第四步,开始编写代码:

这里是布局文件,超简单

<RelativeLayout 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: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.winton.volleytest.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <ImageView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/im_src"
        
        />

</RelativeLayout>

再下面是Activity的代码

package com.winton.volleytest;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity extends Activity {

	
	private RequestQueue volleyClient;
	
	ImageView imageView ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		volleyClient=Volley.newRequestQueue(MainActivity.this);
		imageView=(ImageView)findViewById(R.id.im_src);
		
		downloadImage();
	}
	
	public void downloadImage(){
		String url="http://a.hiphotos.baidu.com/image/pic/item/4afbfbedab64034f30575067a9c379310a551d3c.jpg";
		
		ImageRequest  imr = new ImageRequest(url, new Listener<Bitmap>() {
			@Override
			public void onResponse(Bitmap response) {
				// TODO Auto-generated method stub
				System.out.println(response.getByteCount());
				imageView.setImageBitmap(response);
				
			}
		}, 200, 200, ScaleType.CENTER_INSIDE, Config.ARGB_8888, new ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				// TODO Auto-generated method stub
				System.out.println(error.getMessage());
			}
		});
		
		volleyClient.add(imr);
	}
	
	

}
代码写的很简单,知识初次使用,就先这么多了。

下面运行直接看效果吧


感觉比用httpclient简单多了,有待深入研究。


/*

QQ讨论群组:372702757

*/



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值