繁体   English   中英

不支持POST请求403禁止

[英]POST request 403 forbidden is not supported

我当前正在尝试通过执行REST请求来获取响应JSON,但问题是“当Status为200:OK时,我得到JSON响应,但是当status为403:Forbidden时,我无法获得JSON响应”。 以下是我在200:OK上的工作代码,但没有任何其他状态消息:

接口:

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

有什么建议或解决方案吗? 提前致谢

您没有收到json,但可以检查收到的状态。 您应该使用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必须像这样扩展RestClientErrorHandling

public interface RestClient extends RestClientErrorHandling

然后,在实例化RestClient的位置,应如下设置RestErrorHandler:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM