2

I am using OKHttp dependency.I am not able to pass array to server in NamvaluePair paramenters.

This my function to send parameter in POST and Getting jSON in response.

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) throws IOException, JSONException {

 OkHttpClient client = new OkHttpClient();

    Log.i("PARAMETERS", "PARAMETERS ::" + params);

    FormEncodingBuilder builder = new FormEncodingBuilder();

    for (NameValuePair valuePair : params) {

        builder.add(valuePair.name, URLEncoder.encode(valuePair.value, "UTF-8"));
    }

    Request request = new Request.Builder().url(url).post(builder.build()).build();
    Log.i("Registration Request::", request.toString());

    Response response = client.newCall(request).execute();
    Log.i("REGISTRATION RESPONSE::", response.toString());


       JSONObject jObj= new JSONObject(response.body().string()) ;

 return jObj;
}

This is my Funtion to Call Action.

public JSONObject getShopCategory(String jsonArray) throws IOException {
        // Building Parameters

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new NameValuePair("tag", categoryShopTag));
        params.add(new NameValuePair(categoryShopTag, jsonArray));
        // params.add(new BasicNameValuePair("buyer_id", buyer_id));
        // params.add(new BasicNameValuePair("last_sync_date", last_sync_date));
        Log.i("CATEGORY REQUEST::", params.toString());
        // getting JSON Object
        JSONObject json = null;
        try {
            json = jsonParser.getJSONFromUrl(UrlMap.urlPath, params);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json;
    }

This is from where i am calling the funtion.

for (int i = 0; i < sellerId.size(); i++) {

                lastdateCat = "2011-02-11 18:57:25";


          buyerCategoryFunction = new BuyerCategoryFunction();

                JSONObject jsonObjectCategory = new JSONObject();
                try {
                    jsonObjectCategory.put("shop_id", sellerId.get(i));
                    jsonObjectCategory.put("last_sync_date", lastdateCat);
                    jsonObjectCategory.put("buyer_id", CommonUtilities
                            .getSellerId(getApplicationContext()));

                } catch (JSONException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                jsonCategoryArray.put(jsonObjectCategory);

            }


 JSONObject jsonCategory = buyerCategoryFunction
                            .getShopCategory(jsonCategoryArray.toString();

Issue is not from web service because its working fine with HttpClient. Please help me to go thought.

Nigam Patro
  • 2,760
  • 1
  • 18
  • 33
Niral Bhavsar
  • 519
  • 1
  • 6
  • 13
  • Possible duplicate of [How to add array to okhttp body (POST)](http://stackoverflow.com/questions/33307395/how-to-add-array-to-okhttp-body-post) – Rahul Khurana Dec 13 '16 at 10:55
  • @Niral Bhavsar have got the solution ? If yes please share your code. – Farmer Feb 02 '17 at 10:29

0 Answers0