【问题标题】:Sending Video using HTTP Post Multipart entity使用 HTTP Post Multipart 实体发送视频
【发布时间】:2012-04-20 12:41:28
【问题描述】:

我想要什么我想将视频从我的 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);

我没有例外,没有任何回应。谁能指导我,这里有什么问题?

【问题讨论】:

  • 您是否设置了互联网权限或看到多方jar文件有一些依赖关系,所以您是否正在导入它们.....?
  • @Khawar 你检查你的服务器日志了吗?
  • @Vipin 是的,我已经设置了权限,你能解释一下这个依赖关系吗?可能这就是问题所在。我不知道。
  • @Gaurav 我没有这台服务器。如果有错误,服务器会返回给我一个带有错误的 XML,但没有响应和异常。
  • @Khawar 我希望你正在使用这个 apache-mime4j、httpclient、httpcore 和 httpmime 类,这些类又需要其他类,如果问题仍然存在,我将发送代码并且我用来上传图像到服务器.......

标签: android http-post


【解决方案1】:

我问题中的上述代码是完美的,但我遇到了网络问题。我的设备已连接到热点(Connectify 软件)。当我连接到原始网络时,这段代码运行良好。

我建议大家不要相信热点的这种功能。

【讨论】:

  • 您是如何发现自己在热点上的?
【解决方案2】:

如果想作为内容发送或esle,请尝试使用这种方式,我将在今晚之前上传项目

        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);

【讨论】:

  • 但我也想用它发送参数,否则我会从服务器收到错误。如何使用此代码发送“BasicNameValuePair("video[title]","testvideo")"?
  • 您可以轻松添加基本名称值对,现在可以使用此库dl.dropbox.com/u/58874635/lib.zip
  • 感谢图书馆。我已经包括了所有这些。你能给我一个代码,我可以发送文件和字符串参数吗?
  • 这是我试过的另一个代码,stackoverflow.com/questions/10257426/…
  • 用我的库替换库,您似乎想发送可以通过 httppost 发送的模式...比如您的 url + "/AdServer/getvideooverlaylog?imei=" + WatchDogService.IMEI + "&data =" + 数据 .i,e
猜你喜欢
  • 2014-05-13
  • 2011-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
相关资源
最近更新 更多