如何通过Java BufferedReader从Nexmo.com get-balance API获取正确的XML页面?

时间:2012-05-15 02:30:40

标签: java jsp

我尝试调用Nexmo.com的get-balance web / RESTful API以获得帐户余额,但我收到了两种错误响应:(1)400 - GAE-Java runtme中找不到页面。 (2)Java SE运行时中的FileNotFoundException。如何通过Java BufferedReader获取正确的XML页面?

String api =  "http://rest.nexmo.com/account/get-balance/{idkey}/{pwkey}";
URL url = new URL(api);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
while ((line = reader.readLine()) != null) {
...

3 个答案:

答案 0 :(得分:1)

    String strURL = "http://rest.nexmo.com/account/get-balance/";
    strURL += USERNAME + "/" + PASSWORD;
    GetMethod get = new GetMethod(strURL);
    get.addRequestHeader("Accept", "application/xml");
    HttpClient httpclient = new HttpClient();
    try {
        int result = httpclient.executeMethod(get);
        System.out.println("Response status code: " + result);
        String responseBodyAsString = get.getResponseBodyAsString();
        System.out.println("responseBodyAsString: " + responseBodyAsString);
    } finally {
        get.releaseConnection();
    }

答案 1 :(得分:0)

我相信,如果keysecret丢失,获得404的唯一方法就是:

http://rest.nexmo.com/account/get-balance/[key]/[secret]

你确定他们都被安置了吗?

标准免责声明:我为Nexmo做了一些开发者布道。

答案 2 :(得分:0)

我遇到了同样的问题。您唯一需要做的就是在请求中添加“Accept”标题:

GetMethod get = new GetMethod("https://rest.nexmo.com/account/get-balance/{key}/{secret}");
HttpClient client = new HttpClient();

try {
    get.addRequestHeader("Accept", "application/xml"); //OR "application/json"
    int status = client.executeMethod(get);
    ...

希望它有所帮助!!