0

我有一个应用程序,可以将 PDF 和 PNG 图像文件以及其他一些数据上传到服务器。我使用 Stetho 作为检查器,但每次我尝试打开日志时,它都会挂起然后崩溃,所以我不知道我发送到服务器的内容是否正确。

以下是所需数​​据(方法:POST,内容类型:multipart/form-data):

  • Content-Disposition: form-data name="SrcFile" Content-Type: application/pdf (要上传的PDF文件)
  • 内容处置:表单数据;name="SignFile" Content-Type: image/png (要上传的PNG文件)
  • 模式(字符串值)
  • 请求(字符串值)
  • 令牌(字符串值)

这是我第一次这样做,但根据我的研究,这是我上传上述数据的代码:

final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
   @Override
   public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
   }

   @Override
   public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
   }

   @Override
   public java.security.cert.X509Certificate[] getAcceptedIssuers() {
          return null;
   }
 }
};

// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
OkHttpClient client = new OkHttpClient();
client.setSslSocketFactory(sslContext.getSocketFactory());
client.setHostnameVerifier(new HostnameVerifier() {
       @Override
       public boolean verify(String hostname, SSLSession session) {
              return true;
       }
});
client.networkInterceptors().add(new StethoInterceptor());

File pngFile = new File(Environment.getExternalStorageDirectory() + "signature_" + pdfFile.getFileName() + ".png");
File pdf = new File(Environment.getExternalStorageDirectory() + pdfFile.getFileName());

String mode = "SampleMode"

Headers headers = new Headers.Builder()
        .add("Content-Type", "multipart/form-data")
        .build();

MultipartBuilder multipartBuilder = new MultipartBuilder()
       .type(MultipartBuilder.FORM)
       .addFormDataPart("Req", "SampleRequest")
       .addFormDataPart("Token", "MyAPIToken")
       .addFormDataPart("Mode", mode)
       .addFormDataPart("SrcFile", pdf.getAbsolutePath(), RequestBody.create(MediaType.parse("application/pdf"), pdf))
       .addFormDataPart("SignFile", pngFile.getAbsolutePath(), RequestBody.create(MediaType.parse("image/png"), pngFile));

RequestBody requestBody = multipartBuilder
             .build();

Request request = new Request.Builder()
             .url("MyURLHere")
             .headers(headers)
             .post(requestBody)
             .build();

Response response = client.newCall(request).execute();

我只是想知道我是否正确地进行了上传,或者我设置了一些错误的参数。同样,我似乎无法使用 Stetho,因为每当我尝试打开它时它都会挂起。谢谢!

编辑 原来我刚刚切换了 SrcFile 和 SignFile!XD 上传成功,现在开始下载响应。

4

1 回答 1

0

我宁愿建议你使用这个jar

本库的使用

SyncHttpClient client = new SyncHttpClient();
RequestParams param = new RequestParams();

param.put("email", email);
param.put("title", title);

try {
    if (!path.equals("") || path != null) {
        param.put("logo", new File(path));
    } else {
        path = " ";
        param.put("logo", new File(path));
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

String link = "your link";

client.post(link, param, new TextHttpResponseHandler() {

    @Override
    public void onSuccess(int arg0, Header[] arg1, String arg2) {
        success1 = "true";
    }

    @Override
    public void onFailure(int arg0, Header[] arg1, String arg2,
            Throwable arg3) {
        success1 = "false";
    }
});
于 2016-03-16T06:40:35.303 回答