android-async-http学习笔记
2013-09-06 10:37
411 查看
1、post请求发送json字符串:
在服务端,直接使用以下代码得到json数据:
打印的日志如下:
{"username":"ryantang"}
2、上传文件及参数:
3、get请求:
AsyncHttpClient client = new AsyncHttpClient(); Context context = null; try { JSONObject jsonObject = new JSONObject(); jsonObject.put("username", "ryantang"); StringEntity stringEntity = new StringEntity(jsonObject.toString()); client.post(context, "http://api.com/login", stringEntity, "application/json", new JsonHttpResponseHandler() { @Override public void onSuccess(JSONObject jsonObject) { super.onSuccess(jsonObject); } }); } catch (Exception e) { e.printStackTrace(); }
在服务端,直接使用以下代码得到json数据:
String content = IOUtils.toString(request.getInputStream()); System.out.println(content);
打印的日志如下:
{"username":"ryantang"}
2、上传文件及参数:
final File zipFile = new File(logFile.getParentFile() .getAbsolutePath() + File.separatorChar + System.currentTimeMillis() + IMEI + ".zip"); RequestParams params = new RequestParams(); try { ZipUtils.zipFiles(historyLogs, zipFile); params.put("userid", userId); params.put("phone_no", phoneNo); params.put("os_descript", OSDESCRIPT); params.put("os_version", OSVER); params.put("oem", OEM); params.put("version_id", String.valueOf(versionCode)); params.put("version_name", versionName); params.put("package_name", packageName); params.put("app_name", appName); params.put("file", zipFile);//文件 } catch (IOException e) { e.printStackTrace(); } new AsyncHttpClient().post(serviceUrl, params, new AsyncHttpResponseHandler() { @Override public void onFinish() { Log.i("uploadHistory", "onFinish"); zipFile.delete(); super.onFinish(); } @Override public void onStart() { Log.i("uploadHistory", "onStart"); super.onStart(); } @Override public void onSuccess(String content) { Log.i("uploadHistory", "onSuccess" + content); for (Map.Entry<String, File> file : historyLogs .entrySet()) { if (!file.getKey().equals(logFile.getName())) { file.getValue().delete(); } } super.onSuccess(content); } });
3、get请求:
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"); } }
相关文章推荐
- 【android-async-http】官方文档学习笔记
- Androidx学习笔记(40)--- 使用异步HttpClient(android-async-http-master)做get提交
- android开源框架android-async-http学习笔记一
- Android学习-使用Async-Http实现图片压缩并上传功能
- Android学习(53) -- 使用异步HttpClient--loopj/android-async-http
- 我的 Android 学习笔记-Okhttp 的使用(译)
- Android课堂学习笔记——HttpUrlConnection(四)
- 开源中国源码学习数据篇(一)之android-async-http框架和AsyncTask
- android菜鸟学习笔记24----与服务器端交互(一)使用HttpURLConnection和HttpClient请求服务端数据
- Android课堂学习笔记——HttpUrlConnection(三)
- Android之okHttpClient+handler+LruCache缓存网络图片学习笔记(通用MVP模式)
- Android开源框架android-async-http的学习
- Android课堂学习笔记——HttpUrlConnection(二)
- async-http-client开源库学习笔记(一)
- android 学习笔记 HttpURLConnection
- async-http-client开源库学习笔记(一)
- android-async-http开源框架学习
- [Android阅读代码]android-async-http源码学习一
- android-async-http简单封装--笔记
- Android笔记 采用async http 框架 提交数据到服务器demo