我使用HttpURLConnection
张贴到REST API。 我还没有成功发布任何内容,所以我试图找回应该从API是未来的XML响应。
这里是我已经把该代码是给我的问题的部分:
// Process response - need to get XML response back.
InputStream stream = connection.getInputStream();
connection.disconnect();
BufferedReader br = new BufferedReader(stream);
String result;
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
result += line;
}
br.close();
编译器是不愉快的这条线(建议的修复是“流的读者更改类型”):
BufferedReader br = new BufferedReader(stream);
有没有人有任何建议,我怎么可能会做这正常吗? 任何帮助表示赞赏。
完整的代码在这里:
package com.gwt.HelpDeskTest.server;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.gwt.HelpDeskTest.client.HelpDeskTestService;
import com.gwt.HelpDeskTest.shared.HelpDeskTestException;
@SuppressWarnings("serial")
public class HelpDeskTestImpl extends RemoteServiceServlet implements HelpDeskTestService {
@Override
public String postToRemoteServer(String serviceUrl) throws HelpDeskTestException {
try {
final String serverPath= "http://helpdesk.rmi.org/sdpapi/request/";
final String serverParameters = "OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=D4ADD3A3-9CD4-4307-932B-29E96BCFA5B6&INPUT_DATA=%3C?xml%20version=%25221.0%2522%20encoding=%2522utf-8%2522?%3E%3COperation%3E%3CDetails%3E%3Crequester%3EBetsy%20Leach%3C/requester%3E%3Csubject%3ETest%3C/subject%3E%3Cdescription%3ETesting%20curl%20input%20again%3C/description%3E%3C/Details%3E%3C/Operation%3E"; // Put parameters here for testing.
// trying HttpURLConnection instead of a plain URLConnection
URL url = new URL(serverPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(serverParameters.getBytes().length));
connection.setUseCaches (false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(serverParameters);
wr.flush();
wr.close();
// Process response - need to get XML response back.
InputStream stream = connection.getInputStream();
connection.disconnect();
// Put output stream into a String
BufferedReader br = new BufferedReader(stream);
String result;
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
result += line;
}
br.close();
System.out.println(result);
return result;
}
catch (final Exception e) {
System.out.println(e.getMessage());
throw new HelpDeskTestException();
}
}
}