Android_访问网络三(android-async-http框架的简单使用)

PS:看了9年的小说,自己开始动手写了一本,请各位猿们动动手指,点击下,有起点账号的可以收藏下!!《武意长存》


android-async-http是一个开源的框架,它可使我们更加简便的进行网络的访问。

下载地址  https://github.com/loopj/android-async-http

服务端的代码以及乱码问题一样参见 之前的文章,这里不再赘述。


Android_访问网络三(android-async-http框架的简单使用)_第1张图片

点击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框架的简单使用)_第2张图片

android-async-http是一个异步的http框架,我们无须在自己手动的开启线程,而且我们可以直接在onSuccess和onFailure方法中执行一些与UI相关的操作,比较简单方便。


你可能感兴趣的:(Android)