2013-03-20 94 views
0

我使用Sentiment-140提供的公共API来查找小文本是正面,负面还是中性。虽然我可以成功地使用他们简单的HTTP-JSON服务,但我没有使用CURL。这是我的代码:在JAVA中接收来自CURL的响应

public static void makeCURL(String jsonData) throws MalformedURLException, ProtocolException, IOException { 
    byte[] queryData = jsonData.getBytes("UTF-8"); 

    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.1.1.1", 8080)); 
    HttpURLConnection con = (HttpURLConnection) new URL("http://www.sentiment140.com/api/bulkClassifyJson").openConnection(proxy); 
    con.setRequestMethod("POST"); 
    con.setDoOutput(true); 

    OutputStream os = con.getOutputStream(); 
    InputStream instr = con.getInputStream(); 
    BufferedReader br = new BufferedReader(new InputStreamReader(instr)); 

    os.write(queryData); 
    os.close(); 
    String lin; 
    while((lin = br.readLine())!=null){ 
     System.out.println("[Debug]"+lin); // I expect some response here But it's not showing anything    
    } 
} 

我在做什么错?

+3

您的代理是否需要验证?有什么异常?堆栈跟踪 ? – 2013-03-20 06:03:24

+0

没有身份验证。我可以成功地使用类似的技术来获得常规的HTTP响应... – Aditya 2013-03-20 06:31:57

+0

尝试移动这些行:InputStream instr = con.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(instr));在os.write()和os.close()之后。 – Kylar 2013-03-20 16:09:12

回答

0

您应该在获得输入连接流之前发送您的所有请求数据。

byte[] queryData = jsonData.getBytes("UTF-8"); 
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.1.1.1", 8080)); 
HttpURLConnection con = (HttpURLConnection) new URL("http://www.sentiment140.com/api/bulkClassifyJson").openConnection(proxy); 
con.setRequestMethod("POST"); 
con.setDoOutput(true); 
OutputStream os = con.getOutputStream(); 
os.write(queryData); 
os.close(); 

InputStream instr = con.getInputStream(); 
BufferedReader br = new BufferedReader(new InputStreamReader(instr)); 
String lin; 
while((lin = br.readLine())!=null){ 
    System.out.println("[Debug]"+lin); 
}