我想使用MailChimp api添加订阅者。首先,想从一个REST中读取信息,我正试图从MailChimp api中获得响应。
当我的状态为200时,我似乎正在正确地进行授权,但是由于某种原因,我没有得到响应。
这是到目前为止的代码:
public void doPostAction() throws IOException{
// BASIC Authentication
String name = "user";
String password = apikey;
String authString = name + ":" + password;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
URL urlConnector = new URL(url);
HttpURLConnection httpConnection = (HttpURLConnection) urlConnector.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
httpConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
httpConnection.setRequestProperty("Accept", "application/json");
httpConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
InputStream is = httpConnection.getInputStream();
// check status
System.out.println("DoPost: status: " + httpConnection.getResponseCode());
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
System.out.println("DoPost response: \n" + line);
br.close();
}
看着MailChimp游乐场,似乎我很想念...
我如何获得回复?
**** /编辑/ **** 如果有人在看上面的代码,输出应该是:
System.out.println("DoPost response: \n" + sb); // not line
答案 0 :(得分:0)
确定,以上代码有效。基本错误。
我正在检查line变量为null时的值,而不是响应...
当我更改为:
System.out.println("DoPost response: \n" + line); // not line
System.out.println("DoPost response: \n" + sb); // but sb StringBuilder
...有效。