0

我正在使用 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            
    }
}

我究竟做错了什么?

4

1 回答 1

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);
}
于 2013-04-04T07:23:50.423 回答