使用HTTP Post Multipart实体发送视频

时间:2012-04-20 12:41:29

标签: android http-post

我想要什么我想将SD卡中的视频发送到服务器。我也想用它发送一些参数/值。

我试过我尝试过以下代码:

public String SendToServer(String aUrl,File aFile)
    {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(aUrl);

        try 
        {
            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            entity.addPart("file", new FileBody(aFile));
            entity.addPart("video[title]", new StringBody("testVideo"));
            entity.addPart("video[type]", new StringBody("1"));
            httpPost.setEntity(entity);

            HttpContext localContext = new BasicHttpContext();
            // Bind custom cookie store to the local context
            localContext.setAttribute(ClientContext.COOKIE_STORE, Globals.sessionCookie);

            HttpResponse response = httpClient.execute(httpPost, localContext);
            HttpEntity resEntity = response.getEntity();  
            String Response = "";
            if (response != null) 
            {    
                Response = EntityUtils.toString(resEntity); 
            }
            return Response;
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }

        return "Exception";
    }

有什么问题当我运行此代码时,我遇到了问题

HttpResponse response = httpClient.execute(httpPost, localContext);

我也不例外,根本没有回应。谁能指导我,这里有什么问题?

2 个答案:

答案 0 :(得分:4)

我的问题中的上述代码是完美的,但我遇到了网络问题。我的设备已连接到热点(Connectify Software)。当我连接到原始网络时,此代码完美无缺。

我建议人们永远不要相信热点这种功能。

答案 1 :(得分:1)

如果想要以内容形式发送,请尝试使用此方式,否则我将在今晚上传项目

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(filePath), -1);
        reqEntity.setContentType("binary/octet-stream");
        reqEntity.setChunked(true); // Send in multiple parts if needed
        httppost.setEntity(reqEntity);
        HttpResponse response = httpclient.execute(httppost);