不支持POST请求403 forbidden

时间:2016-07-19 10:24:51

标签: android json rest android-annotations

我目前正在尝试通过执行REST请求来获取响应JSON,但问题是"当状态为200时:好然后我得到JSON响应但当状态为403时:禁止然后我无法获得JSON响应& #34 ;.以下是我在200上的工作代码:好但不在任何其他状态消息上:

接口

@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)

任何建议或解决方案?提前谢谢

1 个答案:

答案 0 :(得分:0)

您没有收到json,但您可以查看收到的状态。 您应该使用RestErrorHandler,如上所述here

我的代码示例

@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必须像这样扩展RestClientErrorHandling

public interface RestClient extends RestClientErrorHandling

然后,在实例化RestClient的地方,你应该将RestErrorHandler设置如下:

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

您可以使用HttpStatus.FORBIDDEN更改HttpStatus.UNAUTHORIZED