2015-12-02 34 views
0

如何使用Volley庫發佈參數?我嘗試了很多,但我無法獲得解決方案。所以如果有人知道如何使用抽球發佈參數,那麼請幫助我。如何在Android中使用Volley庫發佈參數?

+0

哪種類型的參數要發佈? – tsiro

+0

我想發佈電子郵件amd密碼使用Android中的post方法.... –

回答

0

您可以創建擴展進口com.android.volley.Request這樣您的自定義請求單(在這種情況下,它是JSON,但你可以使用字符串):

public class CustomRequest extends Request<JSONObject> { 

    private Response.Listener<JSONObject> listener; 
    private Map<String, String> params; 

    public CustomRequest(String url, Map<String, String> params, 
         Response.Listener<JSONObject> reponseListener, Response.ErrorListener errorListener) { 
     super(Method.GET, url, errorListener); 
     this.listener = reponseListener; 
     this.params = params; 
    } 

    public CustomRequest(int method, String url, Map<String, String> params, 
         Response.Listener<JSONObject> reponseListener, Response.ErrorListener errorListener) { 
     super(method, url, errorListener); 
     this.listener = reponseListener; 
     this.params = params; 
    } 

    protected Map<String, String> getParams() 
      throws com.android.volley.AuthFailureError { 
     return params; 
    }; 

    @Override 
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { 
     try { 
      String jsonString = new String(response.data, 
        HttpHeaderParser.parseCharset(response.headers)); 
      return Response.success(new JSONObject(jsonString), 
        HttpHeaderParser.parseCacheHeaders(response)); 
     } catch (UnsupportedEncodingException e) { 
      return Response.error(new ParseError(e)); 
     } catch (JSONException je) { 
      return Response.error(new ParseError(je)); 
     } 
    } 
    @Override 
    protected void deliverResponse(JSONObject response) { 
     // TODO Auto-generated method stub 
     listener.onResponse(response); 
    } 
} 

而且使用這樣的:

Map<String, String> params = new HashMap<String, String>(); 
       params.put("param1",string1); 
       params.put("param2", "string2"); 
       RequestQueue requestQueue = Volley.newRequestQueue(context); 
       CustomRequest jsObjRequest = new CustomRequest(Request.Method.POST, BuildConfig.API_HOSTNAME_JSON_POST, 
         params, reponseListener, errorListener); 
requestQueue.add(jsObjRequest); 
+0

請問你能告訴我爲什麼它在這種情況下顯示空值.http://www.androidhive.info/2014/05/android-working -with-volley-library-1/ –

+0

我不知道這個API在哪裏值實際上工作? –

+0

這是我用過的api ... http://infowiz.in/workforce/login –

0

首先你必須創建一個自定義的類,它擴展了Volley的請求類...在這個類中,你應該用你用來做請求的方法(在你的情況下是POST)聲明一個構造函數,URL終點,具體你會喜歡的參數e發佈,以及Volley回調監聽器。你也需要創建一個方法來獲取post數據將被設定,並從片段的活性送

public class MyRequest extends Request<JSONObject> { 
      private Listener<JSONObject> listener; 
      private Map<String, String> reponseListener; 

      public MYRequest(int method, String url, Map<String, String> params, Listener<JSONObject> reponseListener, ErrorListener errorListener) { 
      super(method, url, errorListener); 
      this.reponseListener = reponseListener; 
      this.params = params; 
      } 

     protected Map<String, String> getParams() 
      throws com.android.volley.AuthFailureError { 
     return params; 
     } 

     @Override 
     protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { 
try { 
    String jsonString = new String(response.data, 
      HttpHeaderParser.parseCharset(response.headers)); 
    return Response.success(new JSONObject(jsonString), 
      HttpHeaderParser.parseCacheHeaders(response)); 
} catch (UnsupportedEncodingException e) { 
    return Response.error(new ParseError(e)); 
} catch (JSONException je) { 
    return Response.error(new ParseError(je)); 
} 
} 

@Override 
protected void deliverResponse(JSONObject response) { 
// TODO Auto-generated method stub 
    responselistener.onResponse(response); 
    } 
    } 

    } 

你可以調用這個類從acctivity這樣的:

RequestQueue requestQueue = VolleySingleton.getInstance().getRequestQueue(); 
    MyRequest jsObjRequest = new MyRequest(Request.Method.POST, LOGIN_URL, params, new Response.Listener<JSONObject>() { 

     @Override 
     public void onResponse(JSONObject response) { 
      Log.d("RESPONSE", response.toString()); 

     } 
    }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      VolleyLog.d("RESPONSE ERROR", "Error: " + error.getMessage()); 

     } 
    }); 

    requestQueue.add(jsObjRequest); 

其中PARAMS哈希是:

Map<String, String> params = new HashMap<String, String>(); 
     params.put("email", "yourEmail"); 
     params.put("password", "yourPassword"); 
+0

如果你不想使用單例模式來實例化RequestQueue,你可以簡單地這樣做:RequestQueue queue = Volley.newRequestQueue(this); – tsiro

+0

感謝代碼現在工作正常..... –

+0

如果你喜歡,你可以接受我的答案... – tsiro

相關問題