2016-09-30 54 views
-2

我使用basic64 auth方法將用戶名和密碼傳遞給url。迴應是一個令牌。我怎樣才能得到這個令牌使用排球庫?如何獲得響應數據形式凌亂解析的http頭請求?

在此先感謝

+0

https://developer.android.com/training/volley/request.html#request-json – BNK

+0

我想你會在這裏找到http://stackoverflow.com/questions/31230308/way-to解決方案-pass-long-parameter-in-url-request-using-volley希望這會對你有所幫助 – Vij

回答

0

您可以通過使用下面的代碼傳遞JsonRequest。

JsonObjectRequest req = new JsonObjectRequest(Url, new JSONObject(), 
       new com.android.volley.Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         try { 
          Log.v("Response:%n %s", response.toString(0)); 
          JSONObject jsonObject = new JSONObject(response.toString()); 
          String success = jsonObject.getString("success"); 

          // Get your Token Here. 

         } catch (JSONException e) { 
          e.printStackTrace(); 
          Toast.makeText(LoginActivity.this, "Server or Connection Error.", Toast.LENGTH_SHORT).show(); 
          builder.dismiss(); 
         } 
        } 
       }, new com.android.volley.Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.e("Error: ", error.getMessage()); 
      } 
     }); 

     AppController.getInstance().addToRequestQueue(req); 

要通過排球請求需要AppController類。

public class AppController extends Application { 

    public static final String TAG = AppController.class.getSimpleName(); 

    private RequestQueue mRequestQueue; 
    private static AppController mInstance; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mInstance = this; 
    } 

    public static synchronized AppController getInstance(){ 
     return mInstance; 
    } 

    public RequestQueue getRequestQueue(){ 
     if(mRequestQueue == null){ 
      mRequestQueue = Volley.newRequestQueue(getApplicationContext()); 

     } 
     return mRequestQueue; 
    } 

    public <T> void addToRequestQueue(Request<T> req, String tag) { 
     // set the default tag if tag is empty 
     req.setTag(TextUtils.isEmpty(tag) ? TAG : tag); 
     getRequestQueue().add(req); 
    } 

    public <T> void addToRequestQueue(Request<T> req) { 
     req.setTag(TAG); 
     getRequestQueue().add(req); 
    } 



    public void cancelPendingRequests(Object tag) { 
     if (mRequestQueue != null) { 
      mRequestQueue.cancelAll(tag); 
     } 
    } 

} 
+0

Im sorry .. but that does not help .. im即使用解析的網絡類傳遞請求 @Override public Map getHeaders()throws AuthFailureError Map headers = new HashMap (); //添加頭文件 String credentials = email +「:」+ password; String auth =「Basic」+ Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); headers.put(「Authorization」,auth); 返回標題; } –

+0

試着用通常的方法取響應..但它給出了一個未知的響應.. ResponseData:[B @ 41a24780 IM正確地得到的StatusCode而主叫response.statuscode .. 但在調用response.data ,我得到未知的迴應(ResponseData:[B @ 41a24780) –