2016-03-25 71 views
0

我試圖用抽球做出發佈請求。無法做出排球jsonarrayrequest

請求參數是一個json數組。

以下是我請求參數

[ 
"Type", 
{ 
    "User": "email", 
    "password": "dimmer", 

} 
] 

我有幀以上JSON和IM使POST請求如下的方法frameJsonArray,

JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(Request.Method.POST, Constants.requestUrl, frameJsonArray(), 
       new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
        Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show(); 


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

       } 
      } 
    ); 

林在上面的代碼線得到錯誤即時提出請求的地方。我怎樣才能得到這個排序?

以下是我的錯誤日誌

Error:(162, 46) error: constructor JsonArrayRequest in class JsonArrayRequest cannot be applied to given types; 
required: String,Listener<JSONArray>,ErrorListener 
found: int,String,JSONArray,<anonymous Listener<JSONObject>>,<anonymous ErrorListener> 
reason: actual and formal argument lists differ in length 

以下是我frameJsonArrayMethod

public JSONArray frameJsonArray() { 
    JSONObject jsonObject = new JSONObject(); 
try { 
    jsonObject.put("login_type", "Android"); 
    jsonObject.put("username", email); 
    jsonObject.put("password", password); 
    jsonObject.put("short_name", null); 
    jsonObject.put("ip","123.421.12.21"); 

} catch (JSONException e) { 
    e.printStackTrace(); 
} 
JSONArray jsonArray = new JSONArray(); 
jsonArray.put("Login"); 
jsonArray.put(jsonObject); 

Log.d(TAG, "he;ll " + jsonArray.toString()); 
Toast.makeText(getApplicationContext(), jsonArray.toString(), Toast.LENGTH_SHORT).show(); 
return jsonArray; 

}

+0

有什麼錯誤? –

+0

我發佈了錯誤日誌 –

+0

,請更正您的json數據。完成電子郵件的雙配額方面。像「電子郵件」 –

回答

1
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(Request.Method.POST, Constants.requestUrl, frameJsonArray(), 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
        Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show(); 


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

      } 
     } 
); 

變化的JSONObject以JSONArray在onResponse()

編輯的錯誤來,因爲只有一個在JsonArrayRequest構造即

public JsonArrayRequest(String url, Listener<JSONArray> listener, ErrorListener errorListener){} 

https://android.googlesource.com/platform/frameworks/volley/+/e7cdf98078bc94a2e430d9edef7e9b01250765ac/src/com/android/volley/toolbox/JsonArrayRequest.java

您使用的是一些構造與5個參數。

+1

仍然會得到相同的錯誤 –

+0

你可以發佈你的代碼frameJsonArray() –

+0

我已經添加它 –

1

一旦檢查你的JSON,格式錯誤,你錯過了雙引號。

[ 
"Type", 
{ 
    "User": "email /** you missed double quote here **/, 
    "password": "dimmer", 

} 
] 
+0

我已糾正它,錯誤是在做出一個JSON數組請求本身 –

+0

接受它的答案,如果它用於你。 – shobhan

+0

實際的錯誤是使尚未解決的jsonarrayrequest –

1
JsonArrayRequest req = new JsonArrayRequest(url, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
        Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();    
        } 
       }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         VolleyLog.d(TAG, "Error: " + error.getMessage()); 
         pDialog.hide(); 
        } 
       }); 

來源:http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

+0

是啊但我怎樣才能發送我的請求參數使用上面的代碼片段? –

+0

http://stackoverflow.com/a/18052417/4854450,但對於API請求,最好使用GSON的Retrofit,它比Volley和ASyncTask更快。 –