【问题标题】:upload with content disposition android使用内容配置 android 上传
【发布时间】:2015-04-20 05:27:01
【问题描述】:

这是我上传mp3的代码

    builder= new StringBuilder(); 
    int TIMEOUT_MILLISEC = 10000;  // = 10 seconds
    HttpClient httpclient = new DefaultHttpClient();


    String urlString=base_url+"/api/greetings/phone/"+userExtensionData.getId();

    //System.out.println("urlString=== "+urlString);

    HttpPost httpPost=new HttpPost(urlString);
    try {



        httpPost.addHeader("app_token", userData.getAppToken());
        httpPost.addHeader("user_token",userData.getUserToken());
        httpPost.addHeader("user_ip",userData.getUserIpAddress());



          String BOUNDARY= "--eriksboundry--";

          httpPost.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
           MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
           File file= new File(path);
           FileBody bin = new FileBody(file);
           reqEntity.addPart("file",bin);
           reqEntity.addPart("greeting[name]", new StringBody("jdjd"));
           reqEntity.addPart("greeting[description]", new StringBody("dddvd"));




           httpPost.addHeader("Accept-Encoding", "gzip, deflate");

           httpPost.setHeader("Accept", "application/json");

        httpPost.setEntity(reqEntity);      


        HttpResponse response= httpclient.execute(httpPost);

        StatusLine statusLine =response.getStatusLine();

        statusCode=statusLine.getStatusCode();
        //System.out.println("status code === "+statusCode);
       if(statusCode ==200||statusCode ==500 ) {
            if(response!=null){
                HttpEntity entity= response.getEntity();
                InputStream content= entity.getContent();
                BufferedReader reader= new BufferedReader(new InputStreamReader(content));
                String line;
                while((line=reader.readLine())!=null){
                    builder.append(line);
                }

                //System.out.println("submitted  ==  "+builder.toString());

            }
       }

    }catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }catch (ClientProtocolException e) {
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }

    return null;

我的问题是当我尝试执行此操作时出现 500 错误,并且内容处置存在问题。 reqEntity.addPart("greeting[name]", new StringBody("jdjd")); reqEntity.addPart("greeting[description]", new StringBody("dddvd")); 我不知道如何将内容分配添加到多部分实体。 谁能帮帮我。 当我试图从高级休息客户端上传它的工作 输入图片描述

我无法从 android 代码中执行相同的操作。

【问题讨论】:

    标签: android upload content-disposition


    【解决方案1】:

    MultipartyEntity 已弃用。我建议你选择它的替代品MultipartEntityBuilder

    这是代码

    MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
    reqEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    File file= new File(path);
    FileBody bin = new FileBody(file);
    reqEntity.addPart("file",bin);
    reqEntity.addPart("greeting[name]", new StringBody("jdjd"));
    reqEntity.addPart("greeting[description]", new StringBody("dddvd"));
    HttpEntity entity = reqEntity.build();
    httpPost.setEntity(entity);
    

    希望对你有帮助!

    【讨论】:

    • :不,它不起作用。我的问题是我必须发送此内容--ARCFormBoundary76ntfhxhxxyldi Content-Disposition: form-data; name="greeting[name]" yhgreetingrrgggddgdd --ARCFormBoundary76ntfhxhxxyldi Content-Disposition: form-data; name="greeting[description]" ddsss --ARCFormBoundary76ntfhxhxxyldi-- with file.how to add content disposition to multipart entity?.
    • 您已经编写了正确的代码来发送文件内容。我们使用 addPart 添加内容
    • 如果你检查我的 2 个屏幕截图,我必须填写该表格,我点击原始选项卡,我应该点击解码有效负载,然后它可以工作。不知道如何在 android 中执行。
    • BufferedReader bufferreader = new BufferedReader(new InputStreamReader(response .getEntity().getContent()));字符串味精="";字符串行 = ""; while ((line = bufferreader.readLine()) != null) { msg += line; } Log.i("msg=",""+msg);
    • 试试上面的代码,告诉我你收到了什么信息
    猜你喜欢
    • 2017-03-12
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 2014-06-02
    • 2016-06-30
    • 2016-07-12
    相关资源
    最近更新 更多