【问题标题】:Android: Downloading XML Data to display in app - getting NullAndroid:下载 XML 数据以在应用程序中显示 - 获取 Null
【发布时间】:2017-09-09 15:36:32
【问题描述】:

我正在尝试从 API 填充 XML 数据但得到空值。我尝试了一些来自 stackoverflow 的解决方案,这是我最新的迭代。为什么我在按下按钮后没有将 XML 数据填充到我的应用程序中?我没有收到任何错误消息。我把它分成两个类。一个类具有 UI 并将用户输入的字符串分隔到为 URL 正确格式化的两个字段中,并具有 AsyncTask。另一个类具有 URL 实例并执行。

 

我知道 url 格式正确,因为我在最后一个上尝试了 println 并从控制台单击它并转到正确的 xml。我使用的测试地址是(街道:2114 Bigelow Ave / zip:98109) 代码如下:

 

GetProperty.java:

 

public class GetProperty {

    public String address;
    //URL with ZWSID
    public static final String myURL = "http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=[REMOVED ID]";
    public String finalUrl;
    String line;
    private static final boolean DEBUG = true;

    public GetProperty(String address) {
        this.address = address;
        finalUrl = myURL + address;
        System.out.println("The FINAL URL string is: " + finalUrl);
        line = "";

    }

    public void load() throws MalformedURLException, IOException
    {
        //Create URL with address from above
        URL  url = new URL(finalUrl);


        //URLConnection connection = url.openConnection();
        InputStream stream = url.openStream();
        BufferedReader br;

        try
        {
            br = new BufferedReader(new InputStreamReader(stream));

            // consume any data remaining in the input stream
            while (br.readLine() != null) {
               line = line + br.readLine(); }

            br.close();         

        }

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

    }

    public String getLine() {
        return line;
    }
}

 

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    EditText getStreetET;
    EditText getZipET;
    Button getInfoBTN;
    TextView xmlTextView;


    String streetAddress;
    String zipAddress;
    String userAddress;
    GetProperty property;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getStreetET = (EditText) findViewById(R.id.editText);
        getZipET = (EditText) findViewById(R.id.editText2);
        xmlTextView = (TextView) findViewById(R.id.textView);
        getInfoBTN = (Button) findViewById(R.id.button);

        //Get information about address user typed in edit text when BUTTON is clicked
        getInfoBTN.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                //Get text from user and change it to a string
                streetAddress = getStreetET.getText().toString();
                //Add a + sign wherever there is a space in the street address
                streetAddress = streetAddress.replaceAll(" ", "+");
                //adding &address= for the URL
                streetAddress = "&address=" + streetAddress;

                zipAddress = "&citystatezip=" + getZipET.getText().toString();

                //Combine street & zip into one string address
                userAddress = streetAddress + zipAddress;
                System.out.println("The user address without the URL is: " + userAddress);

                //Make sure the user actually typed something
                if(!containsWhiteSpace(userAddress)) {
                    getAddressInfoTask addressTask = new getAddressInfoTask();
                    addressTask.execute(userAddress);

                }

                //Test if user typed in correct address?
                else {
                    xmlTextView.setText("");
                }
            }
        });

    }

    public static boolean containsWhiteSpace(String userAddress) {
        if (!hasLength(userAddress)) {
            return false;
        }
        int strLen = userAddress.length();
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(userAddress.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    public static boolean hasLength(String userAddress) {
        return (userAddress != null && userAddress.length() > 0);
    }

    //AsyncTask for separate thread due to internet connection
    private class getAddressInfoTask extends AsyncTask<String, Void, GetProperty>
    {

        protected GetProperty doInBackground(String... params)
        {


            property = new GetProperty(userAddress);
            try
            {
                property.load();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

            return property;
        }

        protected void onPostExecute(GetProperty property)
        {
            //Place the XML in the TextView
            xmlTextView.setText(property.getLine());
        }
    }


}

【问题讨论】:

  • 你试过断点调试吗?
  • 我已尝试使用此工具查看您的 xml:codebeautify.org/xmlviewer(从 URL 加载)。它说:“错误:无效或缺少城市/​​州/邮政编码参数”(代码:501)所以问题出在您的链接

标签: java android xml api zillow


【解决方案1】:

我尝试了将变量 finalUrl 设置为 https://www.google.com 的您的代码,它设法在应用程序中打印 xml。因此,您的代码的检索部分有效。

然后我尝试了一个基于this blog 的请求,并直接在浏览器上使用您的 API 密钥(您建议不要共享它):

http://www.zillow.com/webservice/GetSearchResults.htm?zws-id=X1-ZWz1fz4ljopwjv_4wzn5&address=1600%20Pennsylvania%20Ave&citystatezip=20500

响应是 xml 格式的,但它显示以下内容:

Error: this account is not authorized to execute this API call

您的代码上的完全相同的请求给出了响应null。这是因为错误流和正常流不一样。当 Chrome 发现正常流为空但编码器必须手动执行时,可以在窗口上路由错误。

PS:你应该先使用 postman 来测试 API 上的请求。然后在你确定它有效的 url 上测试它之后在你的程序上测试它。

【讨论】:

  • 嗨,泽维尔。谢谢你的回复,不过我一直追到下半场。 “错误和正常流不一样”是什么意思?当它说“错误:此帐户无权执行此 API 调用”时,您是否表示我需要请求 Zillow 给我一些更高级别的帐户?
  • 是的。我认为您的问题与您的帐户有关。此外,要访问错误流,从您编码的内容中,最简单的是使用 HttpURLConnection 来自android SDK。我让你在文档上看到。
猜你喜欢
  • 2018-01-09
  • 2019-10-15
  • 1970-01-01
  • 1970-01-01
  • 2017-05-30
  • 2012-08-27
  • 1970-01-01
  • 2016-11-06
  • 2015-07-10
相关资源
最近更新 更多