2013-03-20 95 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); 
}