2017-12-23 28 views
0

사용자의 Reddit 프론트 페이지를 얻으려고합니다. Token Retrieval (code flow)을 통해 인증 토큰을 성공적으로 받았습니다. Postman을 통해 예상 된 JSON 응답을 얻을 수 있었지만 Retrofit을 사용하여 동일한 결과를 얻을 수는 없습니다. 콜백에서 onFailure()가 트리거되어 요청이 시간 초과 된 것처럼 보입니다. scope, identity, mysubreddits 및 read를 사용하고 있습니다.Reddit Api GET 요청이 우편 배달부에서 성공했지만 Retrofit으로 실패합니다.

추가 참고 : 범위가 충분하지 않고 만료 된 인증 토큰을 사용할 때 아래 코드로 401 및 403 응답을 받았습니다.

관련 상수 :

redditToken = (actual auth token String) 
RedditConstants.REDDIT_BASE_URL_OAUTH2 = "https://oauth.reddit.com" 

관련 방법 섹션 :

if (redditToken != null) { 
     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl(RedditConstants.REDDIT_BASE_URL_OAUTH2) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 

     Api api = retrofit.create(Api.class); 

     Map<String, String> headers = new HashMap<>(); 
     headers.put("Authorization", "bearer " + redditToken); 
     headers.put("User-Agent", RedditConstants.REDDIT_USER_AGENT); 

     Call<RedditFeed> call = api.getFeed(headers); 
     call.enqueue(new Callback<RedditFeed>() { 
      @Override 
      public void onResponse(Call<RedditFeed> call, Response<RedditFeed> response) { 
       Log.d("FINDME", "response "+ response.toString()); 

       if (response.isSuccessful()) { 
        Log.d("FINDME", "response was a success! we got the feed!"); 
       } else { 
        Log.d("FINDME", "responce was not successfull triggered"); 
       } 
      } 
      @Override 
      public void onFailure(Call<RedditFeed> call, Throwable t) { 
       Log.d("FINDME", "onFailure called from populateRedditFeed"); 
      } 
     }); 
    } else { 
     Toast.makeText(this, "Please Login with Reddit", Toast.LENGTH_SHORT).show(); 
    } 

개조 인터페이스 :

public interface Api { 
    @GET(".") 
    Call<RedditFeed> getFeed (
      @HeaderMap Map<String, String> headers 
    ); 
} 

로그 결과 :

D/NetworkSecurityConfig: No Network Security Config specified, using 
platform default 
I/zygote: Do full code cache collection, code=123KB, data=105KB 
After code cache collection, code=111KB, data=79KB 
D/FINDME: onFailure called from populateRedditFeed 

이죠 성공 :

enter image description here

+0

onFailure에 Throwable을 인쇄하면 오류의 원인을 알 수 있습니다. 우리가 도울 수 있도록 여기에 게시하십시오 – Kushan

+0

코드를 수정하지 않아도됩니다. 나는 지금 성공적인 응답을 얻고있다. 전에 내가 스타 벅스에 있었고 지금은 집에 WiFi를 사용하고 있습니다. 아마 그 일은 그 일과 관련이 있습니다. 내일 스타 벅스에서 다시 테스트 해 보겠습니다. –

+0

하하 cool :) 알아두면 좋겠어. 코드가 괜찮아서 401과 403 코드를 지적 했으므로 인증 토큰에 대해 알고 있다는 것을 알았습니다 ... 그래서 내가 던지기 가능 여부에 대해 물었습니다 로그, 어쨌든 로그를 유지하면 도움이 될 것입니다. – Kushan

답변

0

후 많은 시작과 중지는, 겉으로는 무작위로 점점 중 하나 (200) 또는 내 개조 모델 클래스 중 하나에 문제를 발견() onFailure를 호출. JSON response from Reddit에는 long 또는 boolean 중 하나 일 수있는 필드가 들어 있습니다. 나는 그것이 long으로 반환되었을 때 llegalStateException을 던진 자바 클래스의 부울로 정의했다.

type  name  description 
special edited false if not edited, edit date in UTC epoch-seconds 
        otherwise. NOTE: for some old edited comments on reddit.com, this will 
        be set to true instead of edit date. 

*이 난 현장을 주석 한 예상대로 코드가 작동 지금 있도록 자바 유형의 이중성에 대처하는 방법을 확실 해요.