0

I am facing a problem with BasicNameValuePair. I want to pass a ArrayList into BasicNameValuePair. Through NameValuePair, I want to Post this arrayList to Php file. But I am getting a error. Please help me out. Thank you.

Here is my Android Code:

    protected String doInBackground(String... params) {
            InputStream inputStream = null;
            String result = null;
            Intent in = getIntent();

            String city = in.getStringExtra(("city"));

            ArrayList<String> category = in.getStringArrayListExtra("category");  **//Here i am passing multiple value of category.**
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

            nameValuePairs.add(new BasicNameValuePair("city", city));

            nameValuePairs.add(new BasicNameValuePair("category", category)); **//Here error is coming(BasicNameValuePair cannot applied to <string, java.util.ArrayList<java.lang.String>)**
            DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost("http://10.0.2.2/qsearch.php");
                           try {
                               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                               HttpResponse response = httpClient.execute(httppost);

                               HttpEntity entity = response.getEntity();
                               inputStream = entity.getContent();

                           }
                           catch(Exception e){
                               e.printStackTrace();
                           }
            try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {

                    sb.append(line + "\n");
                }

                result = sb.toString();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {

                try {

                    if (inputStream != null) inputStream.close();
                } catch (Exception squish) {
                }
                return result;
            }
        }
        @Override
        protected void onPostExecute(String result){
            myJSON=result;
            showList();
        }
    }
    GetDataJSON g=new GetDataJSON();
    g.execute();
Priyanka
  • 75
  • 1
  • 12
  • Check [this](http://stackoverflow.com/questions/21131015/how-to-send-a-string-array-as-basic-name-value-pair-as-httppost) – Raghavendra May 05 '16 at 07:37
  • You should not use HttpClient, HttpPost, NameValuePair, ect. http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client – Massimo May 05 '16 at 08:10

1 Answers1

2

Here category is a ArrayList not a String.

Try replacing this line:

nameValuePairs.add(new BasicNameValuePair("category", category));

with this:

for (int i=0 ;i< category.size();i++) {
    nameValuePairs.add(new BasicNameValuePair("category[]", category.get(i)));
}

You may need to replace the key "category" to "category[]" depending on your server.

Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
  • I wander what the server would say receiving many value-pairs with the same `"category"` key. Maybe OP should state what format the server expects. – Carlo Moretti May 05 '16 at 07:48