上传内容配置android

时间:2015-04-20 05:27:02

标签: android upload content-disposition

这是我上传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”)); 我不知道如何将内容处理添加到多部分实体。 任何人都可以帮助我。 当我尝试从高级休息客户端上传它的工作 输入图像说明check here i am able to work out from rest client

but how to add content to that form check near payload

我无法通过Android代码执行相同操作。

1 个答案:

答案 0 :(得分: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);

希望它有所帮助!