天天看點

android開源架構andorid-async-http使用

1、下載下傳: https://github.com/loopj/android-async-http 2、在工程中加入jar包,或直接把源檔案加到工程中 3、android-async-http文檔: http://loopj.com/android-async-http/

AsyncHttp使用回調的方法處得請求的結果。

AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(String response) {
        System.out.println(response);
    }
});
           

最好建一個靜态的AsyncHttpClient

public class HttpUtil {

	private static AsyncHttpClient client = new AsyncHttpClient(); // 執行個體話對象

	static {
		client.setTimeout(10000); // 設定連結逾時,如果不設定,預設為10s
	}

	// 用一個完整url擷取一個string對象
	public static void get(String urlString, AsyncHttpResponseHandler res) {
		client.get(urlString, res);
	}

	// url裡面帶參數
	public static void get(String urlString, RequestParams params,
			AsyncHttpResponseHandler res){
		client.get(urlString, params, res);

	}

	// 不帶參數,擷取json對象或者數組
	public static void get(String urlString, JsonHttpResponseHandler res) {
		client.get(urlString, res);
	}

	// 帶參數,擷取json對象或者數組
	public static void get(String urlString, RequestParams params,
			JsonHttpResponseHandler res) {
		client.get(urlString, params, res);
	}

	// 下載下傳資料使用,會傳回byte資料
	public static void get(String uString, BinaryHttpResponseHandler bHandler) {
		client.get(uString, bHandler);
	}

	public static AsyncHttpClient getClient(){
		return client;
	}

}



下載下傳:
 
    public void downloadClick(View view) {

        String url = "http://f.hiphotos.baidu.com/album/w%	3D2048/sign=38c43ff7902397ddd6799f046dbab3b7/9c16fdfaaf51f3dee973bf7495eef01f3b2979d8.jpg";

        HttpUtil.get(url, new BinaryHttpResponseHandler() {

            @Override
            public void onSuccess(byte[] arg0) {

                super.onSuccess(arg0);
                File file = Environment.getExternalStorageDirectory();
                File file2 = new File(file, "cat");
                file2.mkdir();
                file2 = new File(file2, "cat.jpg");
                try {
                    FileOutputStream oStream = new FileOutputStream(file2);
                    oStream.write(arg0);
                    oStream.flush();
                    oStream.close();
                    textView.setText("可愛的貓咪已經儲存在sdcard裡面");
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("hck", e.toString());
                }

            }

        });

    } 


上傳:
    public void uploadClick(View view){

    	String path="http://10.0.2.2:8080/jsontest/servlet/UploadServlet";
    	File myFile = new File("/sdcard/cat/cat.jpg");
    	RequestParams params = new RequestParams();

    	try {
    	    params.put("filename", myFile);    	   
    	    AsyncHttpClient client = new AsyncHttpClient();
    	    client.post(path, params, new AsyncHttpResponseHandler(){

				@Override
				public void onSuccess(int statusCode, String content) {
					super.onSuccess(statusCode, content);
					Toast.makeText(MainActivity.this, "上傳成功!", Toast.LENGTH_LONG).show();
				}
    	   	    	   	
    	    });
    	   
    	} catch(FileNotFoundException e) {
    		
    	}

    }
           

繼續閱讀