0

我是 Java 新手,我正在尝试创建一个库来从 Web 服务中获取巴西地址,但我无法读取响应。

在类的构造函数中,我有这个result字符串,我想将响应附加到这个字符串中,一旦这个变量填充了响应,我就会知道该怎么做。

问题是:由于某种原因,我猜该BufferedReader对象不工作,所以没有响应被读取:/

这是代码:

package cepfacil;

import java.net.*;
import java.io.*;
import java.io.IOException;

public class CepFacil {
    final String baseUrl = "http://www.cepfacil.com.br/service/?filiacao=%s&cep=%s&formato=%s";
    private String zipCode, apiKey, state, addressType, city, neighborhood, street, status = "";

    public CepFacil(String zipCode, String apiKey) throws IOException {
        String line = "";

        try {
            URL apiUrl = new URL("http://www.cepfacil.com.br/service/?filiacao=" + apiKey + "&cep=" + 
                    CepFacil.parseZipCode(zipCode) + "&formato=texto");

            String result = "";

            BufferedReader in = new BufferedReader(new InputStreamReader(apiUrl.openStream()));

            while ((line = in.readLine()) != null) {
                result += line;
            }
            in.close();

            System.out.println(line);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        this.zipCode = zipCode;
        this.apiKey = apiKey;
        this.state = state;
        this.addressType = addressType;
        this.city = city;
        this.neighborhood = neighborhood;
        this.street = street;
    }
}

所以这是代码应该如何工作的,你构建一个像这样的对象:

String zipCode = "53416-540";
String token = "0E2ACA03-FC7F-4E87-9046-A8C46637BA9D";

CepFacil address = new CepFacil(zipCode, token);

// so the apiUrl object string inside my class definition will look like this:
// http://www.cepfacil.com.br/service/?filiacao=0E2ACA03-FC7F-4E87-9046-A8C46637BA9D&cep=53416540&formato=texto
// which you can check, is a valid url with content in there

为简洁起见,我省略了此代码的某些部分,但构造函数中调用的所有方法都在我的代码中定义,并且没有发生编译或运行时错误。

我很感激你能给我的任何帮助,我很想听听最简单的解决方案:)

提前致谢!

更新:现在我可以解决问题(@Uldz 向我指出问题的巨大道具)它是开源的http://www.rodrigoalvesvieira.com/cepfacil/

4

3 回答 3

1

System.out.println(line + "rodrigo"); 

你输出行而不是结果。也许最后一行是空的?

于 2013-02-12T12:52:14.583 回答
0

可能有多种原因。包裹你URLHttpURLConnection,这将帮助你查看响应代码和更多关于你从服务器获得的响应的信息。

于 2013-02-12T12:46:29.113 回答
0

您可以/应该向 InputStreamReader 添加编码。然后结果不添加换行符。

        BufferedReader in = new BufferedReader(new InputStreamReader(apiUrl.openStream()));

        while ((line = in.readLine()) != null) {
            System.out.println("Line: " + line);
            String[] keyValue = line.split("\\s*=\\s*", 2);
            if (keyValue.length != 2) {
                System.err.println("*** Line: " + line);
                continue;
            }
            switch (keyValue[0]) {
                case "status":
                    status = keyValue[1];
                    break;
                ...
                default:
                    System.err.println("*** Key wrong: " + line);
            }
            result += line + "\n";
        }
        in.close();
于 2013-02-12T12:56:25.473 回答