2017-07-26 72 views
0

所以我想打印響應的結果,然後使用gson爲模型生成數據,但響應永遠不會返回onResponse被調用。Volley Post請求返回沒有響應裏面onResponse

如果您發現Logcat,則onResponse中的log.i未顯示..!?但在使用此請求的Activity的onSuccess內部,它通常會顯示日誌,但是如果日誌包含響應對象,則它不會顯示哪個非常奇怪,也沒有任何意義。

logcat的

I/getUrl:: http://1925.232.55/login.php 
I/getParams:: {username =samy, password=123456} 
D/libc: [NET] android_getaddrinfofornetcontext+,hn 20(0x6),sn(),hints(known),family 0,flags 1024, proc=com... 
D/libc: [NET] android_getaddrinfo_proxy get netid:0 
D/libc: [NET] android_getaddrinfo_proxy-, success 
I/onSuccess:: check /* this log inside the Activity which uses this request */ 

I tried test the request using PostMan with the same url and params and it returns json response normally..?

郵差響應

{ 
    "status": "success", 
    "user_id": "10", 
    "user_name": "samy", 
    "full_name": "samy samy", 
    "picture": "st_pictures/IMG_085207.jpg", 
    "level": "1", 
    "school": "NY School", 
    "city": "NY", 
    "class": "6", 
    "age": "22", 
    "teacher": "2", 
    "token": "f808e758sdfsdfsf162dbdfcf88e3dc8a96" 
} 

請求代碼

final RequestQueue queue = Volley.newRequestQueue(context); 

     final StringRequest sr = new StringRequest(Request.Method.POST, getLoginURL(), new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      Log.i("onResponse: ", response + ""); 
     requestResult.onSuccess(response); 


     }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
     requestResult.onFail(error); 

      VolleyLog.e("Error: ", error.getMessage()); 
      error.printStackTrace(); 
     } 
     }) 
     { 
     @Override 
     protected Map<String, String> getParams() { 
      Map<String, String> params = new HashMap<String, String>(); 
      params.put("username ", un); 
      params.put("password", pass); 
      Log.i("getParams: ", params.toString()); 
      return params; 
     } 

     @Override 
     public byte[] getBody() throws AuthFailureError { 
      Log.i("getUrl: ",getUrl()); 
      return super.getBody(); 

     } 
     } 
     ; 

     queue.add(sr); 

UPDATE#1

URL http://clients.intertech.ps/raz/std.php

E/Volley: [1] 2.onErrorResponse: Error: 
W/System.err: com.android.volley.ParseError: org.json.JSONException: End of input at character 0 of 
W/System.err:  at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:73) 
W/System.err:  at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:123) 
W/System.err: Caused by: org.json.JSONException: End of input at character 0 of 
W/System.err:  at org.json.JSONTokener.syntaxError(JSONTokener.java:449) 
W/System.err:  at org.json.JSONTokener.nextValue(JSONTokener.java:97) 
W/System.err:  at org.json.JSONObject.<init>(JSONObject.java:156) 
W/System.err:  at org.json.JSONObject.<init>(JSONObject.java:173) 
W/System.err:  at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:68) 
W/System.err: ... 1 more 
+0

第一日誌'http://1925.232.55/login.php'我認爲你的網址是錯誤的 –

+0

@OussemaAroua這是一個虛擬的一個,你想讓我用它替換它嗎? –

+0

沒有必要我只是cheking,如果它是真實的或不 –

回答

0

這不是你需要使用JsonObjectRequest是引起你的反應與{

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, 
       url, null, 
       new Response.Listener<JSONObject>() { 

        @Override 
        public void onResponse(JSONObject response) { 
         Log.d(TAG, response.toString()); 
         requestResult.onSuccess(response); 
        } 
       }, new Response.ErrorListener() { 

        @Override 
        public void onErrorResponse(VolleyError error) { 
         VolleyLog.d(TAG, "Error: " + error.getMessage()); 
         requestResult.onFail(error); 
        } 
       }); 

嘗試開始下getParams添加StringRequest

@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"); 
    return params; 
} 

[更新]:

我不知道爲什麼有凌空沒有工作,但我改變ionlink和它的作品:

Ion.with(this) 
       .load("http://clients.intertech.ps/raz/std.php") 
       .setBodyParameter("username", "layal") 
       .setBodyParameter("password", "123456") 
       .asJsonObject() 
       .setCallback(new FutureCallback<JsonObject>() { 
        @Override 
        public void onCompleted(Exception e, JsonObject jsonObject) { 
         Log.e("TAG", "onCompleted: " + jsonObject.toString()); 
        } 
       }); 
+0

我改變它爲JsonObjectRequest,這個錯誤顯示:'E/Volley:[1] 2.onErrorResponse:錯誤:org.json.JSONException:在字符0輸入結束' –

+0

服務器的響應是空的,它什麼也沒有返回 –

+0

但郵遞員確實返回..!?爲什麼這裏不會。? –

0

你必須使用一個JsonObjectRequest,像這樣:

HashMap<String, String> params = new HashMap<String, String>(); 
params.put("username","samy"); 
params.put("password","123456"); 

JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params), 
new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 
      Gson gson = new Gson(); 
      YourClass yourClass = gson.fromJson(response.toString(), yourClass.class); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      //error 
     } 
    }); 
+0

它沒有工作,它給了我同樣的錯誤,因爲'@ OussemaAroua'答案,請參閱完整logcat更新#1 –

+0

你可以仔細檢查你的JSON響應,可能它不是很好形成 – attakon

+0

再次看到更新#1,我將發佈真正的'url',用它在郵遞員的陪伴下自己看。 –