我已将此行添加到build.gradle中
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5'
我想在我的代码中使用MultipartEntityBuilder.但Android Studio不会将库添加到我的代码中.谁能帮我这个?
android gradle apache-httpcomponents multipartentity android-studio
我想将UTF-8编码设置为MultipartEntity对象或StringBody对象.有什么办法吗?我知道如何设置Charset而不是编码.
谢谢.
我正在尝试使用MultipartEntity创建以下服务器POST请求:
parameters: {"parameter1"=>"parameter1", "parameter2"=>{"sub_parameter1"=>"sub_parameter1", "sub_parameter2"=>"sub_parameter2"}}
我目前正在使用类似的东西:
multipartEntity.addPart("parameter1", new StringBody("parameter1"));
FormBodyPart parameter2 = new FormBodyPart("parameter2", new StringBody("")); // It wouldn't allow a null ContentBody
parameter2.addField("sub_parameter1", "sub_parameter1");
parameter2.addField("sub_parameter2", "sub_parameter2");
但是,子字段不会携带.我得到:
parameters: {"parameter1"=>"parameter1", "parameter2"=>""}
如何在MultipartEntity或它包含的FormBodyPart元素中创建嵌套结构?
我想通过安全的restful web服务发送歌曲(mp3/wav)文件和一些数据.我正在使用MultipartEntity来发出HttpPost请求.但是当我通过HttpClient执行它时,服务器会回复此错误
HTTP状态400 - 错误请求类型:状态报告消息:错误请求客户端发送的请求在语法上不正确(错误请求).
但是,如果我们从Web界面调用它,那么该服务的效果非常好.请帮忙
它的代码
HttpClient httpclient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost();
        try {
            MultipartEntity reqEntity = new MultipartEntity();
            reqEntity.addPart("email", new StringBody("test@testmail.com"));
            reqEntity.addPart("password", new StringBody("123"));
            reqEntity.addPart("title", new StringBody("My new song"));
            reqEntity.addPart("musicData", new FileBody(new File(FilePath))); 
            // FIlePath is path to file and contains correct file location
            postRequest.setEntity(reqEntity);
            postRequest.setURI(new URI(ServiceURL));
            HttpResponse response = httpclient.execute(postRequest);
        } catch (URISyntaxException e) {
            Log.e("URISyntaxException", e.toString());
        } 
我还为MultipartEntity提供了apache-mime4j,httpclient,httpcore和httpmime jar.
这是服务的HTML页面捕捉.

我想验证HTTP请求中究竟是什么,即参数和标题.我正在调试的代码在执行HTTP请求之前使用MultiPartEntity来设置setEntity.
response = executePost(multipartEntity);
statusCode = response.statusCode;
我没有从服务器获得预期的响应,因此想要验证发送到服务器的确切事物(url +参数).
谢谢.
我尝试将数据上传到服务器,我的数据包含多个图像和大JSON,在它之前,我尝试使用转换图像发送到字符串使用base64并发送我之前转换的另一个数据和图像JSON,但我OutOfMemory在这里遇到问题,所以我读了一个说我必须尝试使用的解决方案MultipartEntityBuilder.我仍然感到困惑,不明白该怎么做MultiPartEntityBuilder,有没有人可以帮助我这样做MultiPartEntityBuilder?这是我的代码:
    try{
    //membuat HttpClient
    //membuat HttpPost
    HttpPost httpPost= new HttpPost(url);
    SONObject jsonObjectDP= new JSONObject();
    System.out.println("file audio "+me.getModelDokumenPendukung().getAudio());
    jsonObjectDP.put("audio_dp",MethodEncode.EncodeAudio(me.getModelDokumenPendukung().getAudio()));
    jsonObjectDP.put("judul_audio",me.getModelDokumenPendukung().getJudul_audio());
    jsonObjectDP.put("ket_audio",me.getModelDokumenPendukung().getKet_audio());
    JSONArray ArrayFoto= new JSONArray();
    //This loop For my multiple File  Images
    List<ModelFoto>ListFoto=me.getModelDokumenPendukung().getListFoto();
    for (int i=0; i<ListFoto.size();i++) {
        JSONObject jsonObject= new JSONObject();
        jsonObject.put("foto", ListFoto.get(i).getFile_foto());
        jsonObject.put("judul_foto", ListFoto.get(i).getJudul_foto());
        jsonObject.put("ket_foto", ListFoto.get(i).getKet_foto());
        ArrayFoto.put(jsonObject);
    }
    JSONObject JSONESPAJ=null;
     JSONESPAJ = new JSONObject();
     JSONObject JSONFINAL = new JSONObject();
            JSONESPAJ.put("NO_PROPOSAL",me.getModelID().getProposal());
            JSONESPAJ.put("GADGET_SPAJ_KEY",me.getModelID().getIDSPAJ());
            JSONESPAJ.put("NO_VA",me.getModelID().getVa_number());
            JSONESPAJ.put("Dokumen_Pendukung",jsonObjectDP);
            JSONFINAL.put("ESPAJ", JSONESPAJ); …有没有办法限制从org.apache.http.entity.mime.MultipartEntity在MultipartEntity上发送的零件尺寸.我正在使用凌空的网络请求.我试过扩展Volley请求类并重写getbody()方法.
在我在这里发布这个问题之前,我已经尝试添加@Multipart上面的接口方法并且在stackoverflow中搜索仍然找不到类似我的问题.
在这种情况下,我尝试使用TypedFile服务器发送图像.我的界面方法如下所示:
 @Headers({"Content-type: application/json"})
    @POST("/user/change")
    void postChange(@Query("name") String name, @Query("email") String  email, @Query("password") String password, @Query("phone") String phone, @Query("user_id") String userId, @Query("address[]") String[] listAddress, @Query("head[]") String[] head, @Part("photo_profile") TypedFile photoProfile, @Body TypedInput jsonObject, Callback<ReceiveDTO> callback);
编辑
在那种方法中,我们可以看到@Part和@Body.如果我添加@Multipart上面的方法,我们将抛出一个错误@Body parameters cannot be used with form or multi-part encoding. (parameter #9)
我正在使用Retrofit 1.9
我使用Apache HttpClient来发布多个文件服务器.这是代码:
    public static HttpResponse stringResponsePost(String urlString, String content, byte[] image,
        HttpContext localContext, HttpClient httpclient) throws Exception {
    URL url = new URL(URLDecoder.decode(urlString, "utf-8"));
    URI u = url.toURI();
    HttpPost post = new HttpPost();
    post.setURI(u);
    MultipartEntity reqEntity = new MultipartEntity();
    StringBody sb = new StringBody(content, HTTP_CONTENT_TYPE_JSON, Charset.forName("UTF-8"));
    ByteArrayBody ib = new ByteArrayBody(image, HTTP_CONTENT_TYPE_JPEG, "image");
    reqEntity.addPart("interview_data", sb);
    reqEntity.addPart("interview_image", ib);
    post.setEntity(reqEntity);
    HttpResponse response = null;
    response = httpclient.execute(post, localContext);
    return response;
}
问题是,MultipartEntityclass只有isChunked()方法(总是返回false),如果我希望为我的multipart实体启用chucked编码,则没有"setChunked(boolean)"选项.
我的问题是:
HTTP multipart和chunking可以根据协议规范共存吗?如果不是,为什么像其他实体 …
我发送的一种HTTP请求的响应是multipart/form-data看起来像:
--------boundary123
Content-Disposition: form-data; name="json"
Content-Type: application/json
{"some":"json"}
--------boundary123
Content-Disposition: form-data; name="bin"
Content-Type: application/octet-stream
<file data>
--------boundary123
我一直在使用apache发送和接收HTTP请求,但我似乎无法找到一种简单的方法来使用它来解析上面的内容以便于访问表单字段.
我宁愿不重新发明轮子,所以我正在寻找一个允许我做类似的工作的库:
MultipartEntity multipart = new MultipartEntity(inputStream);
InputStream bin = multipart.get("bin");
有什么建议?
multipartentity ×10
android ×8
java ×5
http ×4
http-post ×2
apache ×1
gradle ×1
http-chunked ×1
httpclient ×1
httprequest ×1
json ×1
parsing ×1
retrofit ×1