我希望通过我的Android应用程序使用以下代码上传视频。
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uri);
MultipartEntity entity = new MultipartEntity();
ContentBody input = new FileBody(file);
ContentBody name = new StringBody("VID_20130201_162220.3gp");
ContentBody description = new StringBody("Test Description");
entity.addPart("input",input);
entity.addPart("name",name);
entity.addPart("description",description);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
当httpClient.execute()方法返回响应时,它显示“HTTP / 1.1 415不支持的媒体类型”
我也试过从HttpPost中删除MultipartEntry但仍然会出现同样的错误。
有人可以告诉我可能是什么问题。
答案 0 :(得分:0)
您需要告诉服务器允许3gp媒体类型。如果您正在使用Apache,则可以通过将AddType video/3gpp .3gp
添加到服务器的配置文件来执行此操作。 (我认为这是正确的语法)。
答案 1 :(得分:0)
尝试此解决方案:它适用于大多数类型的文件。
private DefaultHttpClient mHttpClient;
Context context;
public String error = "";
//Contrutor para que metodos possam ser usados fora de uma activity
public HTTPconector(Context context) {
this.context = context;
}
public HTTPconector() {
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
mHttpClient = new DefaultHttpClient(params);
}
public void FileClientPost(String txtUrl, File file){
try
{
error = "";
HttpPost httppost = new HttpPost(txtUrl);
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("Image", new FileBody(file));
httppost.setEntity(multipartEntity);
mHttpClient.execute(httppost, new PhotoUploadResponseHandler());
}
catch (Exception e)
{
Log.e(HTTPconector.class.getName(), e.getLocalizedMessage(), e);
e.getStackTrace();
error = e.getMessage();
}
}
//Verifica se a rede esta disponível
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
// if no network is available networkInfo will be null
// otherwise check if we are connected
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
public String Get(String txtUrl){
try {
URL url = new URL(txtUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(10000);
con.setConnectTimeout(15000);
con.setRequestMethod("GET");
con.setDoInput(true);
con.connect();
return readStream(con.getInputStream());
} catch (ProtocolException e) {
e.printStackTrace();
return "ERRO: "+e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "ERRO: "+e.getMessage();
}
}
public String Post(String txtUrl){
File image;
try {
URL url = new URL(txtUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.connect();
//con.getOutputStream().write( ("name=" + "aa").getBytes());
return readStream(con.getInputStream());
} catch (ProtocolException e) {
e.printStackTrace();
return "ERRO: "+e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "ERRO: "+e.getMessage();
}
}
//Usado para fazer conexão com a internet
public String conectar(String u){
String resultServer = "";
try {
URL url = new URL(u);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
resultServer = readStream(con.getInputStream());
} catch (Exception e) {
e.printStackTrace();
resultServer = "ERRO: "+ e.getMessage();
}
Log.i("HTTPMANAGER: ", resultServer);
return resultServer;
}
//Lê o resultado da conexão
private String readStream(InputStream in) {
String serverResult = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
serverResult = reader.toString();
} catch (IOException e) {
e.printStackTrace();
serverResult = "ERRO: "+ e.getMessage();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
serverResult = "ERRO: "+ e.getMessage();
}
}
}
return serverResult;
}
private class PhotoUploadResponseHandler implements ResponseHandler<Object>
{
@Override
public Object handleResponse(HttpResponse response)throws ClientProtocolException, IOException {
HttpEntity r_entity = response.getEntity();
String responseString = EntityUtils.toString(r_entity);
Log.d("UPLOAD", responseString);
return null;
}
}