[英]How to check when a request has been sent?
我正在發送一個Volley StringRequest,它需要花費三分鍾才能收到響應(但響應狀態正常並且接收操作方法正常工作)。
有什么方法可以檢查請求何時實際上已經發送到服務器(我發現有關Volley請求的所有答案都在談論收到響應的時候),所以我可以判斷這只是我的手機花了2分鍾到甚至在傳輸之前處理請求,或者如果requestQueue在它甚至達到我的請求之前花了很長時間(盡管我只發送一個Volley請求,點擊,優先級設置為IMMEDIATE)。
服務器端日志顯示先前的請求(webview表單提交)在StringRequest之前2-3分鍾到達,但我不確定延遲是在cloudflare端,ELB還是其他地方,我想知道是否我可以將源設備丟棄為有問題。
代碼如下:
public void sendPostRequest(Map<String,String> params, String option)
{
RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
String url="";
switch (option){
//...
case "dniImages": //This is the one
url = BuildConfig.REST_DNI_IMAGES_ENDPOINT ;
break;
}
StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject resp = new JSONObject(response);}
catch(JSONException e){}
}
}
, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
switch (option){
//...
case "dniImages":
Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_SHORT).show();
break;
}
}
}) {
@Override
public Priority getPriority() {
return Priority.IMMEDIATE;
}
@Override
protected com.android.volley.Response<String> parseNetworkResponse(NetworkResponse response) {
if (response.statusCode == 200) {
if (option.equals("dniImages"))
Toast.makeText(getApplicationContext(), "Error uploading images", Toast.LENGTH_SHORT).show();
}
return super.parseNetworkResponse(response);
}
protected Map<String, String> getParams() {
return params;
}
@Override
public Map<String, String> getHeaders(){
try {
Map<String, String> headers = super.getHeaders();
if (headers == null
|| headers.isEmpty()) {
headers = new HashMap<String, String>();
}
this.addSessionCookie(headers);
return headers;
}
catch (AuthFailureError e)
{
}
return new HashMap<String, String>();
}
/**
* Adds session cookie to headers if exists.
* @param headers
*/
public final void addSessionCookie(Map<String, String> headers) {
String cookies = CookieManager.getInstance().getCookie(BuildConfig.TIENDAMIA_URL);
headers.put("cookie", cookies);
}
};
MyRequestQueue.add(MyStringRequest);
}
說實話,我沒有和Volley一起工作過,但是最近我們有一些與網絡相關的錯誤,其中一個幫我很多的工具是Android Profiler 。 我的意思是看看Network Profiler概述這絕對是驚人的。
連接視圖:列出在所有應用程序的CPU線程中所選時間線部分期間發送或接收的文件。 對於每個請求,您可以檢查大小,類型,狀態和傳輸持續時間。 您可以通過單擊任何列標題對此列表進行排序。 您還會看到時間軸選定部分的詳細明細,顯示每個文件的發送或接收時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.