Niels Vanwingh
Niels Vanwingh

Reputation: 604

HTTP Get Request - Incompatible Types

I am running a basic Get Request based on the Yahoo Weather API. It is the example from Youtube.. I cannot get it to run.. I get a runtime error in my HTTP Request in the doInBackground Method. (see underneath).

I get the error message on my phone "Value True of Type Java.lang.boolean cannot be converted into JSON Object". So I need to return a String. BUT when i change the type of "line" into String it gives the error "Incompatible types - required java.lang.String - found Boolean". So the readline command from BufferedReader expects a String but finds a Boolean.. Can anyone explain to me what happens and how to fix this?

Thanks on beforehand!

public void refreshWeather (final String location){

    new AsyncTask<String, Void, String>()

    {
        @Override
        protected String doInBackground(String... strings) {

            String YQL = String.format("select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\"%s\")",location);
            String endpoint = String.format("https://query.yahooapis.com/v1/public/yql?q=%s&format=json", Uri.encode(YQL));

            try {
                URL url = new URL(endpoint);
                URLConnection connection = url.openConnection();
                InputStream inputStream = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder result = new StringBuilder();
                boolean line;

                while ((line = reader.readLine()!= null)){
                    result.append(line);
                }

                return result.toString();

            } catch (Exception e) {
                error = e;
                }
            return null;
        }

        /**

         onPostExecute checks first if there is a service failure, then if city is valid, then it
         populates the data from the city and goes to method serviceSuccess and populates the fields with the retrieved data

         */

        @Override
        protected void onPostExecute(String s) {

            if (s == null && error != null){
                callback.serviceFailure(error);
                return;
            }

            try {
                JSONObject data = new JSONObject(s);
                JSONObject queryResults = data.optJSONObject("query");

                int count = queryResults.optInt("count");
                if (count == 0){

                    callback.serviceFailure(new LocationWeatherException("No Weather Info Found for" + location));
                    return;
                }


                Channel channel = new Channel();
                channel.populate(queryResults.optJSONObject("results").optJSONObject("channel"));

                callback.serviceSuccess(channel);

                } catch (JSONException e) {

                callback.serviceFailure(e);
            }
        }
    }.execute(location);
}

Upvotes: 1

Views: 469

Answers (2)

Rohit
Rohit

Reputation: 146

Your while loop seems to be wrong. "line" should be of type string the while loop should be

while ((line = reader.readLine()) != null)

Upvotes: 1

wanderer0810
wanderer0810

Reputation: 420

When you do a comparison in java, you check with ==. Do not set a variable when doing a comparison either, ex: line = reader.readLine()!= null

This will cause an error as you are trying to set the variable name when you check to see what it is equal to.

Try setting the variable before you call it, then use line != null ex:

line = reader.readLine();
if(line != null) {
    //do something
}

Upvotes: 1

Related Questions