天天看點

Android_通路網絡三(android-async-http架構的簡單使用)

PS:看了9年的小說,自己開始動手寫了一本,請各位猿們動動手指,點選下,有起點賬号的可以收藏下!!《武意長存》

android-async-http是一個開源的架構,它可使我們更加簡便的進行網絡的通路。

下載下傳位址  https://github.com/loopj/android-async-http

服務端的代碼以及亂碼問題一樣參見 之前的文章,這裡不再贅述。

Android_通路網絡三(android-async-http架構的簡單使用)

點選loginByAsynHttpClientGet調用以下方法

public void loginByAsynHttpClientGet(View view){
		final String username = etUsername.getText().toString().trim();
		final String password = etPassword.getText().toString().trim();
		
		if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
			Toast.makeText(this, "使用者名或密碼不能為空!", Toast.LENGTH_SHORT).show();
		}else {
			AsyncHttpClient client = new AsyncHttpClient();
			
			String path = null;
			try {
				path = "http://110.89.138.144:8080/server/LoginServlet?username="+URLEncoder.encode(username, "utf-8")+"&password="+URLEncoder.encode(password, "utf-8");
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
			
			client.get(path, new AsyncHttpResponseHandler() {
				
				@Override
				public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
					try {
						Toast.makeText(MainActivity.this, "請求成功:"+new String(responseBody,"utf-8").toString(), Toast.LENGTH_SHORT).show();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				@Override
				public void onFailure(int statusCode, Header[] headers,
						byte[] responseBody, Throwable error) {
					try {
						Toast.makeText(MainActivity.this, "請求失敗:"+new String(responseBody,"utf-8").toString(), Toast.LENGTH_SHORT).show();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}
	}
           

點選loginByAsynHttpClientPost調用以下方法

public void loginByAsynHttpClientPost(View view){
		final String username = etUsername.getText().toString().trim();
		final String password = etPassword.getText().toString().trim();
		
		if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
			Toast.makeText(this, "使用者名或密碼不能為空!", Toast.LENGTH_SHORT).show();
		}else {
		
			AsyncHttpClient client = new AsyncHttpClient();
			
			//指定要送出的資料實體
			RequestParams params = new RequestParams();
			params.add("username", username);
			params.add("password", password);
			
			String path = "http://110.89.138.144:8080/server/LoginServlet";
			client.post(path, params, new AsyncHttpResponseHandler() {
				@Override
				public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
					if(200 == statusCode){
						try {
							Toast.makeText(MainActivity.this, new String(responseBody, "utf-8").toString(), Toast.LENGTH_SHORT).show();
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}
					}
				}
				@Override
				public void onFailure(int statusCode, Header[] headers,
						byte[] responseBody, Throwable error) {
					try {
						Toast.makeText(MainActivity.this, new String(responseBody, "utf-8").toString(), Toast.LENGTH_SHORT).show();
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
				}
			});
						
		}
	}
           
Android_通路網絡三(android-async-http架構的簡單使用)

android-async-http是一個異步的http架構,我們無須在自己手動的開啟線程,而且我們可以直接在onSuccess和onFailure方法中執行一些與UI相關的操作,比較簡單友善。

繼續閱讀