2016-07-19 17 views
0

現在、RESTリクエストを実行してJSONを取得しようとしていますが、問題は「ステータスが200の場合はOKですが、JSONレスポンスを取得しますが、ステータスが403の場合: JSONレスポンスを取得する "以下は、200の私の作業コードです:他のステータスメッセージでOKではない:POSTリクエスト403はサポートされていません

インタフェース:

@Rest(rootUrl = "http://something.com", converters = {FormHttpMessageConverter.class, GsonHttpMessageConverter.class}) 
public interface RestClient { 
    @Post("/isec/api/user/login") 
    JsonObject LoginIt(@Field String email, @Field String password, @Field String type); 
} 

用途:

JsonObject p = restClient.LoginIt(emailText.getText().toString(),passwordText.getText().toString(),spinner1.getSelectedItem().toString().toUpperCase()); 
      GsonStr = new Gson().toJson(p); 
      ShowResults(p); 

ログイン:

E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1 
        Process: com.jutt.isec, PID: 3463 
        org.springframework.web.client.HttpClientErrorException: 403 Forbidden 
         at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:88) 
         at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:585) 
         at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:541) 
         at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:499) 
         at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:447) 
         at com.jutt.isec.RestClient_.LoginIt(RestClient_.java:41) 
         at com.jutt.isec.MainActivity.doLogin(MainActivity.java:130) 
         at com.jutt.isec.MainActivity_.access$201(MainActivity_.java:31) 
         at com.jutt.isec.MainActivity_$5.execute(MainActivity_.java:159) 
         at org.androidannotations.api.BackgroundExecutor$Task.run(BackgroundExecutor.java:405) 
         at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) 
         at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
         at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152) 
         at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265) 
         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
         at java.lang.Thread.run(Thread.java:818) 

いずれかの提案または解決策?ありがとうございます。

+0

禁止されている403を取得すると、json応答は開始されません。だからjsonとしてそれを処理しようとしないでください。 – greenapps

+0

それから、例外や何かでこれをどう処理するのですか?そうでなければ、私のアプリケーションはクラッシュしています。 –

+0

try catchブロックにコールを入れます。その後、アプリケーションはクラッシュしません。アプリケーションがクラッシュしている場合は、原因をLogCatで見つけることができます。したがって、どの例外がスローされるのかを知ることができます。あなたにはどの例外があるか教えてください。 – greenapps

答えて

0

jsonは届いていませんが、受信したステータスを確認できます。 here

私のコードの例を述べたように あなたは、RestErrorHandlerを使用する必要があります

@EBean 
public class RestErrorHandler implements RestErrorHandler { 

    @RootContext 
    Context context; 

    @Bean 
    MyErrorHandler mErrorHandler; 

    @Bean 
    OttoBus ottoBus; 

    @Override 
    public void onRestClientExceptionThrown(NestedRuntimeException e) { 

     if (e instanceof HttpStatusCodeException) { 
      HttpStatusCodeException exception = (HttpStatusCodeException) e; 
      if (exception.getStatusCode() == HttpStatus.UNAUTHORIZED) { 
     mErrorHandler.handleGenericError(context.getString(R.string.auth_error)); 
      } else { 
       mErrorHandler.handleGenericError(context.getString(R.string.generic_error)); 
      } 
     } else if (e instanceof RestClientException) { 
      // do something else 
     } else { 
      mErrorHandler.handleGenericError(context.getString(R.string.root_error)); 
      Log.e("errorw", e.getMessage()); 
     } 
    } 
} 

あなたRestClientは、あなたがしなければならない、あなたはRestClientをインスタンス化次に、この

public interface RestClient extends RestClientErrorHandling 

、同じようRestClientErrorHandling拡張する必要がありますRestErrorHandlerを以下のように設定します。

@AfterInject 
public void init() { 
    mService.setRestErrorHandler(mErrorHandler); 
} 

HttpStatus.UNAUTHORIZEDをHttpStatus.FORBIDDENと変更することができます

関連する問題