2017-12-13 8 views
1

Yahoo Weather API를 기반으로 기본 Get Request를 실행 중입니다. 그것은 유튜브에서 예제입니다 .. 나는 그것을 실행할 수 없습니다 .. 내 HTTP 요청에 doInBackground 메서드에서 런타임 오류가 발생합니다. (아래 참조).HTTP Get Request - Incompatible Types

내 전화기에서 "Value True of Type Java.lang.boolean을 JSON 개체로 변환 할 수 없습니다."라는 오류 메시지가 나타납니다. 그래서 String을 반환해야합니다. 하지만 "라인"유형을 String으로 변경하면 "호환되지 않는 유형 - 필수 java.lang.String - found Boolean"오류가 발생합니다. 그래서 BufferedReader의 readline 명령은 String을 기대하지만 Boolean을 찾습니다. 아무도 나에게 어떤 일이 일어나고 어떻게 해결할 수 있습니까?

미리 감사드립니다.

공공 무효 refreshWeather (최종 문자열 위치) {

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); 
} 
+0

당신이 어떤 라인에 이러한 오류가 발생할 로그 캣에서 확인하실 수 있습니다 : 당신이 그것을 호출하기 전에

line != null 예를 사용하여 변수를 설정하려고? – tobifasc

답변

0

해야 문자열 형식이어야합니다. 비교를 수행 할 때 변수를 설정하지 마십시오. 예 : line = reader.readLine()!= null

변수 이름이 같을 때 변수 이름을 설정하려고하면 오류가 발생합니다.

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

나는 그것을 쪼개었다. 그리고 지금 그것은 일한다! 답장을 보내 주셔서 감사합니다. –

+0

@NielsVanwingh Great! 내 솔루션이 도움이 되었다면 upvote 카운터 아래의 작은 체크 표시를 클릭하여 허용 된대로 내 대답을 표시 할 수 있습니까? – wanderer0810

1

귀하의 while 루프는 잘못된 것 같다. "라인은"while 루프는 자바 비교를 할 때, 당신은 == 확인

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

예, 정말로 그것을 해결하려고했습니다. 문제는 "String"으로 변경할 수 없어 다른 오류 메시지가 나타납니다. 메소드는 반환 할 문자열을 기다리고있는 것처럼 보입니다. 해결책은 그것들을 나눠서 IF 루프로 바꾸는 것이 었습니다. 여러분의 노력과 도움에 감사드립니다! –