2

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

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

4

2 回答 2

4

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

我建议你们不要相信热点的这种功能。

于 2012-05-02T15:44:48.623 回答
1

如果想作为内容或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);
于 2012-04-20T13:33:32.283 回答