android-async-http学习笔记

1、post请求发送json字符串:

		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");
   }
   
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值