我似乎无法弄清楚如何让 android 注释休息客户端工作我有 2 个主要问题。
A)如何解析通用的json响应并获取有意义的key
B)如何添加参数
对于第一个问题,所有响应都返回为格式如下的 json 字符串
{"success":,"message":"","data":{}}
成功是 bool 值消息是一个字符串,数据将是我要解析的主要数据,它可以是 bool 值、数组、字符串或整数
我很确定我需要拦截响应并处理代码,但我不确定该怎么做
让我们使用看起来像这样的真实响应
{"success":true,"message":"random message","data":{"profile":{"id":"44","user_id":"44","name":"Matt","username":"mitch","icon":"b1da7ae15027b7d6421c158d644f3220.png","med":"2a3df53fb39d1d8b5edbd0b93688fe4a.png","map":"b7bfed1f456ca4bc8ca748ba34ceeb47.png","background":null,"mobile_background":null}}
首先在我的拦截器中我想看看 bool 键“success”是否为真然后返回数据值
@EBean public class RestInterceptor implements ClientHttpRequestInterceptor { final String TAG = "rest"; @Bean AuthStore authStore; @Override public ClientHttpResponse intercept(HttpRequest request, byte[] data, ClientHttpRequestExecution execution) throws IOException{ //Need to set the api key here but nothing happens code quits // Log.d("Rest",authStore.getApiKey()); HttpHeaders headers = request.getHeaders(); headers.set("api_key",""); ClientHttpResponse resp = execution.execute(request, data); HttpStatus code = resp.getStatusCode(); if(code.value() == 200){ Log.d(TAG,"success code 200"); //valid http request but is it a valid API request? //perform some logic of if success == true in root json object //if true cast return data key } else{ Log.d(TAG,"fail code" + code.toString()); } return resp; } }
第二个问题是使用具有 api key 和 session key 的 http 请求发送参数,我这样定义应用程序类
@EApplication public class MyApp extends Application { final String TAG = "app"; @Bean AuthStore authStore; @RestService RestClient restClient; public void onCreate() { super.onCreate(); init(); } @AfterInject public void init() { authStore.setApiKey("dummy_key"); Log.d(TAG, "api key set to " + authStore.getApiKey()); } }
像这样的 AuthStore 类
@EBean(scope = Scope.Singleton) public class AuthStore { public String apiKey,sessionKey; public String getApiKey() { return apiKey; } public void setApiKey(String apiKey) { this.apiKey = apiKey; } public String getSessionKey() { return sessionKey; } public void setSessionKey(String sessionKey) { this.sessionKey = sessionKey; } }
基本上,我在单例中的应用程序级别设置了一个虚拟 api key ,我应该能够在其余拦截器界面中访问它,但代码只是退出而没有错误我基本上遵循本指南 https://github.com/excilys/androidannotations/wiki/Authenticated-Rest-Client
最后,我有一个 Activity 类,它注入(inject)了对其余 http 类和 authstore 类有引用的应用程序依赖项
@EActivity(R.layout.activity_login) public class LoginActivity extends Activity { @App MyApp app; @ViewById TextView email; @ViewById TextView password; @ViewById Button loginButton; @AfterInject public void init() { Log.d(app.TAG, "api in login key set to " + app.authStore.getApiKey()); } @Click @Trace void loginButton() { login(email.toString(), password.toString()); } @Background void login(String email, String password) { app.restClient.forceLogin(); } }
很抱歉,如果信息太多,我已经搜索了一段时间,但无法弄清楚!
提前致谢
最佳答案
我不知道你正在使用的库(注释,spring),但在我看来你正在努力解析 success = true 因为它不应该在 JSON 中。
JSON 最好代表您的应用程序中的一个类,这样您就可以轻松地将其映射到一个对象中。 你的应用程序和网络服务之间的通信,关于请求的状态应该进入标题。
像这样,您可以在解析 JSON 之前检查请求的 header 。
关于android - 试图让 Android 注释 spring RESTful 客户端 api 工作,几个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18528220/