0

所以我想使用 TestFairy API(网址:https ://app.testfairy.com/api/upload )。此 API 调用需要 3 个 post 参数:

  • api_key(细绳)

  • apk_file(.apk 文件)

  • testers_groups(细绳)

到目前为止,我想出了这个:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);

List <NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("testers_groups", testers));
params.add(new BasicNameValuePair("api_key", key));

httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("apk_file", new FileBody(file));

httppost.setEntity(reqEntity);

System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();

System.out.println(response.getStatusLine());
if (resEntity != null) {
    System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
    resEntity.consumeContent();
}

httpclient.getConnectionManager().shutdown();

但它不起作用。

谁能指出我正确的方向?

4

1 回答 1

0

尝试这样的事情:

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("api_key", new StringBody(key));
entity.addPart("apk_file", new FileBody(file));
entity.addPart("testers_groups", new StringBody(testers));
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity);

HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpPost);

StringBody可以选择在and构造函数中添加 mime 类型FileBody(但可能不是必需的)。

于 2013-11-28T08:47:21.737 回答