2016-09-30 53 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) –