2017-02-14 32 views
0

在我的應用程序中,我使用排列庫來獲取json數據。 下面是我的代碼的參數傳遞給Web服務:如何使用volley庫將參數傳遞給Web服務並獲取json數組響應?

private void getProductName(final String id) { 

    pDialog.setMessage("Please wait.."); 
    pDialog.setTitle("Loading"); 
    showDialog(); 

    JsonArrayRequest req = new JsonArrayRequest(Constants.DARSHAN_URL+Constants.PRODUCT_NAME, 
      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 
        Log.d(TAG, response.toString()); 

        try { 
         // Parsing json array response 
         // loop through each json object 
         for (int i = 0; i < response.length(); i++) { 

          JSONObject person = (JSONObject) response.get(i); 

          String material_id = person.getString("material_id"); 
          String material_desc = person.getString("material_desc"); 

          HashMap<String, String> maim_category_array = new HashMap<>(); 

          maim_category_array.put("material_id", material_id); 
          maim_category_array.put("material_desc", material_desc); 

          productName.add(maim_category_array); 

         } 

         adapter = new GridviewAdapter(ProductNameActivity.this, productName); 
         grid.setAdapter(adapter); 
         grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
          @Override 
          public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { 

           HashMap<String, String> map = (HashMap<String, String>)arg0.getItemAtPosition(position); 

           Toast.makeText(ProductNameActivity.this, map.get("material_id"), Toast.LENGTH_LONG).show(); 
           Intent i = new Intent(ProductNameActivity.this, SubProductActivity.class); 
           i.putExtra("BREADCRUMB1", map.get("material_desc")); 
           i.putExtra("MATERIAL_ID", map.get("material_id")); 
           i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
           startActivity(i); 
          } 
         }); 

         // txtResponse.setText(jsonResponse); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
         Toast.makeText(getApplicationContext(), 
           "Error: " + e.getMessage(), 
           Toast.LENGTH_LONG).show(); 
        } 

        hideDialog(); 
       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      VolleyLog.d(TAG, "Error: " + error.getMessage()); 
      Toast.makeText(getApplicationContext(), 
        error.getMessage(), Toast.LENGTH_SHORT).show(); 
      hideDialog(); 
     } 
    }) { 

     @Override 
     protected Map<String, String> getParams() { 
      // Posting parameters to login url 
      Map<String, String> params = new HashMap<String, String>(); 
      params.put("material_id", id); 

      return params; 
     } 

    }; 

    // Adding request to request queue 
    DarshanApplication.getInstance().addToRequestQueue(req); 
} 

我傳遞的material_id的URL。但得到迴應爲com.android.volley.servererror

如何解決問題? 請幫忙!!

+2

不'servererror'意味着有服務器錯誤嗎?還有哪個確切的服務?哪些錯誤代碼和消息? –

+0

調試完代碼後,我得到的錯誤只是作爲錯誤:com.android.volley.servererror。錯誤消息是空的。 – Android

+0

@Sweekar檢查收到的錯誤響應。這會告訴你它出錯的地方。使用先進的休息客戶端工具來檢查您的參數 – Stallion

回答

0

我不知道你爲什麼想使用排球,但也有另一種方法。我使用java和android的方法來獲取JSON並顯示它們。 你可以看看這裏

» JsonFetch
+0

我有將參數發送到Web服務方法的問題。不與提取JSON。 – Android

+0

哦!你也可以用同樣的方法發佈到服務器。你只需要發佈請求方法。 –

相關問題