import com.android.volley.RequestQueue;
import com.android.volley.Response;
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.StringRequest;
import com.android.volley.toolbox.Volley;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text = (TextView) findViewById(R.id.text);
final ImageView image = (ImageView) findViewById(R.id.image);
//第一步: 创建Volley队列
RequestQueue request = Volley.newRequestQueue(this);
//第二步:访问对象 ,String类型
StringRequest stringRequest = new StringRequest("http://www.kan300.com", new Listener<String>() {
/*
*
* onResponse 连接正确时显示
*/
@Override
public void onResponse(String response) {
text.setText(response);
}
}, new ErrorListener() {
/*
*
* onErrorResponse 连接错误时显示
*/
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "错误!", Toast.LENGTH_SHORT).show();
}
});
//访问对象: image类型
ImageRequest imageRequest = new ImageRequest("http://avatar.csdn.net/9/7/A/1_zhangphil.jpg", new Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
image.setImageBitmap(response);
}
}, 0, 0, Config.RGB_565, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//第三步:将访问对象添加到队列中
request.add(stringRequest);
request.add(imageRequest);
}
}
</span>
Android 第三方开源:Volley通过网络下载数据
最新推荐文章于 2022-05-02 09:25:32 发布