Android网络请求库——android-async-http使用
AsyncHttpClient client = new AsyncHttpClient(); client.get("http://www.baidu.com", new AsyncHttpResponseHandler() { @Override public void onSuccess(String response) { System.out.println(response); textView.setText(response); } @Override public void onStart() { super.onStart(); System.out.println("onStart"); } @Override public void onFinish() { super.onFinish(); System.out.println("onFinish"); } }通过Get请求指定的URL并通过回调函数处理请求结果,同时,请求方式还支持POST和PUT,请求的同时还支持参数传递,下面看看如何通过JSON字符串作为参数访问服务器:
try { JSONObject jsonObject = new JSONObject(); jsonObject.put("username", "ryantang"); StringEntity stringEntity = new StringEntity(jsonObject.toString()); client.post(MainActivity.this, "http://api.com/login", stringEntity, "application/json", new JsonHttpResponseHandler(){ @Override public void onSuccess(JSONObject jsonObject) { super.onSuccess(jsonObject); } }); } catch (JSONException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }
官方推荐的使用方法,使用一个静态的请求对象。
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) { // TODO Auto-generated method stub super.onSuccess(statusCode, content); Toast.makeText(MainActivity.this, "上传成功!", Toast.LENGTH_LONG).show(); } }); } catch(FileNotFoundException e) { } }
由于涉及网络请求,最后别忘了添加权限:
<uses-permission android:name="android.permission.INTERNET" />
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。