在JAVA中收到CURL的回复

时间:2013-03-20 05:56:03

标签: java json http curl sentiment-analysis

我正在使用Sentiment-140提供的公共API来查找小文本是正面,负面还是中性。虽然我可以成功使用他们简单的HTTP-JSON服务,但我没有使用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            
    }
}

我做错了什么?

1 个答案:

答案 0 :(得分: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);
}