2016-05-17 19 views
0

我想发送一个HTTP POST请求中的视频url,但它不适合我,我想我已经(几乎?)必要的代码,使其工作,否则我错过了很简单的事情?在Android应用程序没有数据的HTTP POST

public void postVideoURL() throws IOException { 

    String encodedUrl = URLEncoder.encode("http://video.ted.com/talks/podcast/DavidBrooks_2011.mp4", "UTF-8"); 
    URL obj = new URL("http://10.50.0.105:8060/launch/dev?url="+encodedUrl); 

    HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 

    //add request header 
    con.setRequestMethod("POST"); 

    // Send post request 
    con.setDoOutput(true); 
    OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream()); 
    System.out.println(con.getResponseCode()); 
    System.out.println(con.getResponseMessage()); 
    wr.flush(); 
    wr.close(); 

    wr.write(""); 
    } 

任何提示,让我走向正确的方向?

这里是我想要做的,但在C#

using System.Net; 
using System.Web; 

class Program 
{ 
    static void Main() 
    { 
     string rokuIp  = "192.168.0.6"; 
     string channelId = "dev"; 
     string videoUrl = "http://video.ted.com/talks/podcast/DavidBrooks_2011.mp4"; 

     // Note that the query string parameters should be url-encoded 
     string requestUrl = $"http://{rokuIp}:8060/launch/{channelId}?url={HttpUtility.UrlEncode(videoUrl)}"; 

     using (var wc = new WebClient()) 
     { 
      // POST the query string with no data 
      wc.UploadString(requestUrl, ""); 
     } 
    } 
} 

下面的命令后在终端的作品的使用,实际上,这就是我想做的事,但在Java: 卷曲-d “”“http://10.50.0.46:8060/launch/12?url=http%3A%2F%2Fvideo.ted.com%2Ftalks%2Fpodcast%2FDavidBrooks_2011.mp4

+0

定义*不工作* – njzk2

+0

该应用程序启动,但视频不被发送到应用程序,没有错误显示出来的代码,我发送视频到应用程序在电视机上。 (我正在使用connectionsdk作为Android应用程序) – UnknownDistance

+0

您正在以get参数的形式写入视频网址。没关系,如果服务器想这样。 – greenapps

回答

0

你从不写任何东西到输出流。您必须致电wr.write()将数据写入流。

另外,你不能像String那样编码URL。在您单独编码网址后,您需要将两个字符串连接在一起。就像这样:

String encodedUrl = URLEncoder.encode("http://video.ted.com/talks/podcast/DavidBrooks_2011.mp4"); 
URL obj = new URL("http://10.50.0.105:8060/launch/dev?url="+encodedUrl); 
+0

无法解析方法写入?我需要其他东西来完成这项工作吗? – UnknownDistance

+0

您必须提供要写入的数据。所以你应该调用wr.write(“你想要发送到服务器的一些数据”);' – NoChinDeluxe

+0

但是我想要POST是obj的url,我把整个url放到wr.write()中?将网址写入(urlhere)不起作用 – UnknownDistance

相关问题