2016-11-08 75 views
0

在onResponse中的一個字符串發佈請求登錄。我返回的所有狀態代碼是200.我想獲取返回的數據。我不知道從這一點出發應該走哪條路?關於如何獲取數據的任何想法,而不僅僅是狀態碼?Android Volley onResponse獲取數據

public void requestWithSomeHttpHeaders() { 

    try { 
     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     String URL = "http://......"; 
     JSONObject jsonBody = new JSONObject(); 
     jsonBody.put("username", "yourusername"); 
     jsonBody.put("password", "yourpassword"); 
     final String mRequestBody = jsonBody.toString(); 

     StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       Log.i("VOLLEY", response); 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e("VOLLEY", error.toString()); 
      } 
     }) { 
      @Override 
      public String getBodyContentType() { 
       return "application/json; charset=utf-8"; 
      } 

      @Override 
      public byte[] getBody() throws AuthFailureError { 
       try { 
        return mRequestBody == null ? null : mRequestBody.getBytes("utf-8"); 
       } catch (UnsupportedEncodingException uee) { 
        VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8"); 
        return null; 
       } 
      } 

      @Override 
      protected Response<String> parseNetworkResponse(NetworkResponse response) { 
       String responseString = ""; 
       if (response != null) { 
        responseString = String.valueOf(response.statusCode); 
        // can get more details such as response.headers 
        System.out.println(responseString); 
       } 
       return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response)); 
      } 
     }; 

     requestQueue.add(stringRequest); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 
+0

當時真的實現比'onResponse'和'onError'更多的必要嗎? –

回答

1

優選地,定義一種方法。

private void doLogin() { 
    // TODO: startActivity? 
} 

然後調用

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      Log.i("VOLLEY", response); 

      doLogin(); 
     } 

如果你想從服務器上的數據,這就是String response是。檢查你的Logcat。

正如評論中所述,使用StringRequest而未執行getBodyparseNetworkResponsegetBodyContentType可能會更好。

+0

在logcat中,我看到的是I/VOLLEY:200 – SoundCheese

+0

如果我在Google Postman中做類似的事情,我會返回所有類型的數據。 – SoundCheese

+0

我不知道你的URL是什麼,但它似乎只是返回'「200」'。如果這是一個問題,這是所有服務器端,不是Volley問題 –

1

如果你想解析JSON這樣

public Profile getUserProfile(String response){ 
    Profile profile = null; 
    try { 
     profile = new Profile(); 
     JSONObject jsonObject = new JSONObject(response); 
      profile.setId(jsonObject.getInt(context.getString(R.string.profile_id))); 
     profile.setLastName(jsonObject.getString(context.getString(R.string.profile_lastName))); 
     profile.setName(jsonObject.getString(context.getString(R.string.profile_name))); 
     profile.setEmail(jsonObject.getString(context.getString(R.string.profile_email))); 


    } catch (JSONException | NullPointerException e) { 
     e.printStackTrace(); 
     return null; 
    } 

    return profile; 
} 

添加方法在你onResponse方法

 @Override 
     public void onResponse(String response) { 
      //If you have son object 
      Profile profile = getUserProfile(response) 
     }