Java 如何在Android中返回parseNetworkResponse中的状态码?
如何返回api的状态码 我知道状态代码是在parseNetworkResponse中生成的,但是我使用parseNetworkResponse保存缓存,我怎么还能返回状态代码呢?有没有其他方法可以通过截击来做到这一点 这是我的密码Java 如何在Android中返回parseNetworkResponse中的状态码?,java,android,android-volley,Java,Android,Android Volley,如何返回api的状态码 我知道状态代码是在parseNetworkResponse中生成的,但是我使用parseNetworkResponse保存缓存,我怎么还能返回状态代码呢?有没有其他方法可以通过截击来做到这一点 这是我的密码 int status_code = 0; public void loadAnnouncement(){ final Constant WebConfig = new Constant(); RequestQueue
int status_code = 0;
public void loadAnnouncement(){
final Constant WebConfig = new Constant();
RequestQueue queue = Volley.newRequestQueue(getContext());
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, WebConfig.url, null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
try {
JSONArray details = response.getJSONArray("data");
for (int i=0; i<details.length(); i++) {
JSONObject object = details.getJSONObject(i);
announcementList.add(new AnnouncementModel(
object.getInt("a"),
object.getString("b"),
object.getString("c"),
object.getString("d"),
object.getString("e"),
object.getString("f")
));
//creating adapter object and setting it to recyclerview
adapter = new AnnouncementAdapter(getActivity(), announcementList);
announcementRecyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
//This is for Headers If You Needed
@Override
public Map< String, String > getHeaders() throws AuthFailureError {
Map < String, String > params = new HashMap< String, String >();
params.put("Content-Type", "application/x-www-form-urlencoded");
params.put("X-API-KEY", WebConfig.test);
params.put("Authorization", WebConfig.test1);
return params;
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
if (cacheEntry == null) {
cacheEntry = new Cache.Entry();
}
final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background
final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
long now = System.currentTimeMillis();
final long softExpire = now + cacheHitButRefreshed;
final long ttl = now + cacheExpired;
cacheEntry.data = response.data;
cacheEntry.softTtl = softExpire;
cacheEntry.ttl = ttl;
String headerValue;
headerValue = response.headers.get("Date");
if (headerValue != null) {
cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
headerValue = response.headers.get("Last-Modified");
if (headerValue != null) {
cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
cacheEntry.responseHeaders = response.headers;
final String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString), cacheEntry);
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(JSONObject response) {
super.deliverResponse(response);
}
@Override
public void deliverError(VolleyError error) {
super.deliverError(error);
}
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
return super.parseNetworkError(volleyError);
}
};
queue.add(getRequest);
}
int status\u code=0;
公告{
最终常数WebConfig=新常数();
RequestQueue=Volley.newRequestQueue(getContext());
JsonObjectRequest getRequest=新的JsonObjectRequest(Request.Method.GET,WebConfig.url,null,
新的Response.Listener()
{
@凌驾
公共void onResponse(JSONObject响应)
{
试一试{
JSONArray details=response.getJSONArray(“数据”);
对于(int i=0;i getHeaders()抛出AuthFailureError{
Mapparams=newhashmap();
参数put(“内容类型”、“应用程序/x-www-form-urlencoded”);
参数put(“X-API-KEY”,网络配置测试);
参数put(“授权”,WebConfig.test1);
返回参数;
}
@凌驾
受保护的响应parseNetworkResponse(NetworkResponse响应){
试一试{
Cache.Entry cacheEntry=HttpHeaderParser.parseCacheHeaders(响应);
if(cacheEntry==null){
cacheEntry=new Cache.Entry();
}
final long cachehitbutrefresh=3*60*1000;//3分钟后将命中缓存,但也会在后台刷新
final long cacheExpired=24*60*60*1000;//24小时后此缓存项完全过期
long now=System.currentTimeMillis();
最终长时间softExpire=now+cacheHitButRefreshed;
最终长ttl=现在+缓存过期;
cacheEntry.data=response.data;
cacheEntry.softTtl=softExpire;
cacheEntry.ttl=ttl;
字符串头值;
headerValue=response.headers.get(“日期”);
if(headerValue!=null){
cacheEntry.serverDate=HttpHeaderParser.parseDateAsEpoch(headerValue);
}
headerValue=response.headers.get(“上次修改”);
if(headerValue!=null){
cacheEntry.lastModified=HttpHeaderParser.parseDateAsEpoch(headerValue);
}
cacheEntry.responseHeaders=response.headers;
最终字符串jsonString=新字符串(response.data,
HttpHeaderParser.parseCharset(response.headers));
返回Response.success(新的JSONObject(jsonString)、cacheEntry);
}捕获(不支持的编码异常e){
返回Response.error(新的ParseError(e));
}捕获(JSONException e){
返回Response.error(新的ParseError(e));
}
}
@凌驾
受保护的void deliverResponse(JSONObject响应){
super.deliverResponse(响应);
}
@凌驾
公共无效交付错误(截击错误){
super.deliverError(错误);
}
@凌驾
受保护的截击错误解析NetworkError(截击错误截击错误){
返回super.parseNetworkError(截击错误);
}
};
添加(getRequest);
}
这是工作,但我仍然需要返回它的状态代码
非常感谢您的帮助。在parseNetworkResponse方法中,您可以获得响应代码response.statusCode;Hi@MridulSKumar是的,我知道,但是我如何通过parseNetworkResponse实现?考虑到我正在返回缓存?我想如果您也返回NetworkResponse,您可以从中获得状态代码