【发布时间】: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