Exemple #1
0
 // post请求带入参,以String字符串返回请求结果
 private void postString() {
   String url = "http://192.168.1.102:8080/Test/HelloServlet";
   Map<String, String> params = new HashMap<String, String>();
   params.put("username", "zhangsa");
   params.put("password", "123456");
   afeitaNet.post(
       url,
       params,
       new NetCallback<String>() {
         @Override
         public void onResult(String response) {
           Toast.makeText(
                   NetSampleActivity.this,
                   "get HelloServlet post 调用成功:" + response,
                   Toast.LENGTH_LONG)
               .show();
         }
       });
 }
Exemple #2
0
  // post请求,参数有文件流(文件上传)与普通字符串参数。响应是json,以指定类的实例对象返回请求结果
  private void postJsonFile() {
    // 简化,实际要判断Environment.getExternalStorageState()状态,及sdcard中是否存在111.png与222.png
    File file1 = new File(Environment.getExternalStorageDirectory(), "111.jpg");
    File file2 = new File(Environment.getExternalStorageDirectory(), "222.jpg");
    if (file1.exists()) {
      L.e("file1存在,大小:" + file1.length());
    } else {
      L.e("file1不存在,大小:");
    }

    if (file2.exists()) {
      L.e("file2存在,大小:" + file2.length());
    } else {
      L.e("file2不存在,大小:");
    }

    String url = "http://192.168.1.102:8080/Test/UploadHandleServlet";
    RequestInfo requestInfo = new RequestInfo();
    requestInfo.url = url;
    requestInfo.setParams("eamil", "*****@*****.**");
    requestInfo.setParams("mobileNum", "18688888888"); // 普通字符串参数
    requestInfo.setParams("file1", file1); // 上传的文件1
    requestInfo.setParams("file2", file2); // 上传的文件2
    requestInfo.isShowLoadingDialog = false;
    afeitaNet.post(
        requestInfo,
        new NetCallback<String>() {
          @Override
          public void onResult(String response) {
            Toast.makeText(
                    NetSampleActivity.this,
                    "get HelloServlet post 调用成功:" + response,
                    Toast.LENGTH_LONG)
                .show();
          }

          @Override
          public void onFinish(boolean isCancel) {
            if (null != dialog) {
              dialog.dismiss();
              dialog = null;
            }
          }

          // -----可选监听文件上传进度,并显示进度
          @Override
          public void onUpload(
              int fileNum,
              String currentUploadingFilename,
              long sumSize,
              long sumDonedSize,
              long sumSpendedTime) {
            if (null == dialog) {
              dialog = new UploadFileProgreeDialog(NetSampleActivity.this);
              dialog.show();
            }
            dialog.setFileTip(currentUploadingFilename);
            int precent = (int) ((sumDonedSize * 1.0 / sumSize) * 100);
            String strSumSize = Formatter.formatFileSize(NetSampleActivity.this, sumSize);
            String strSumDonedSize = Formatter.formatFileSize(NetSampleActivity.this, sumDonedSize);
            String strProgress = strSumDonedSize + "/" + strSumSize;
            dialog.setProgress(precent, strProgress, precent + "%");
          }
        });
  }