开源框架 Asynchttpclient


package com.example.asynchttpclient;

import org.apache.http.Header;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
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 ImageView image = (ImageView) findViewById(R.id.image);
		final TextView text=(TextView) findViewById(R.id.text);
		
		AsyncHttpClient httpclient = new AsyncHttpClient();
		
		String url = "http://pic16.nipic.com/20110908/6910138_102825181129_2.jpg";
		
		httpclient.get(url,new AsyncHttpResponseHandler(){

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				Toast.makeText(getApplicationContext(), "失败!", Toast.LENGTH_SHORT).show();
					
			}
			@Override
			public void onSuccess(int statusCode, Header[] headers,byte[] response) {
				BitmapFactory bf = new BitmapFactory();
				Bitmap bitmap = bf.decodeByteArray(response, 0,response.length);
				image.setImageBitmap(bitmap);
				
			}	
		} );
	
		
		
		String uri="http://www.baidu.com";
		httpclient.get(uri, new AsyncHttpResponseHandler(){

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				
				Toast.makeText(getApplicationContext(), "失败~!", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers,  
                    byte[] response) {
			
				text.setText(new String(response));
				
			}});
	}
}


<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"
    tools:context="com.example.asynchttpclient.MainActivity" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            
        <ImageView
        		 android:id="@+id/image"
       			 android:layout_width="match_parent"
       			 android:layout_height="match_parent"
       			 android:layout_weight="1"/>

        <TextView
                android:id="@+id/text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"/>
        
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

<uses-permission android:name="android.permission.INTERNET"/>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值