1

I am trying to populate XML data from an API but getting Null. I tried a number of solutions from stackoverflow and this is my latest iteration. Why am I not getting XML data populated into my app after pressing button? I am not getting any error messages. I separated this into two classes. One class has the UI and separates the string the user entered into the two fields properly formatted for a URL and has an AsyncTask. The other class has the URL instance and executes.

 

I know the url is formatting correctly as I tried a println on the final one and clicked on it from console and it went to the proper xml. The test address I used was (street: 2114 Bigelow Ave / zip: 98109) Here is the code:

 

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());
        }
    }


}
j doe
  • 73
  • 3
  • 11
  • 1
    did you try debugging with break points? – Raghunandan Sep 09 '17 at 15:41
  • 1
    I have tried to view your xml by using this tool: https://codebeautify.org/xmlviewer (load from URL). It says: "Error: invalid or missing city/state/ZIP parameter" (code: 501) so the problem is with your link – EtherPaul Sep 09 '17 at 15:41

1 Answers1

0

I tried your'e code with variable finalUrl set to https://www.google.com and it managed to print the xml in the app. So the retrieving part of your code works.

I then tried a request based on this blog and using your API key (you would recommend not to share it) like this one, directly on the browser:

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

The response is xml formatted but it says the following:

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

The exact same request on on your code give the response null. This is because the error and the normal stream is not the same. Chrome can route the error on the window when it finds that the normal stream is null but the coder has to do it manually.

PS: you should use postman to test requests on API first. Then test it on your program after having tested it on an url you are sure it works.

  • Hi Xavier. Thanks for your reply, I was following along until the second half though. What do you mean by "the error and the normal stream is not the same" ? And when it said "Error: this account is not authorized to execute this API Call" are you indicating I need to request Zillow give me some higher level account? – j doe Sep 09 '17 at 18:57
  • Yes. I think your problem is with your account. Also, to access the error stream, from what you have coded, the most simple is to use HttpURLConnection from android SDK. I let you see on the documentation. – Xavier Melendez Sep 09 '17 at 19:14