如何将ArrayList <string>传递给BasicNameValuePair

时间:2016-05-05 07:27:59

标签: java android arraylist basicnamevaluepair

我遇到了BasicNameValuePair的问题。我想将ArrayList传递给BasicNameValuePair。通过NameValuePair,我想将这个arrayList发布到Php文件。但是我收到了一个错误。请帮帮我。谢谢。

这是我的Android代码:

    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();

1 个答案:

答案 0 :(得分:1)

此类别是ArrayList而不是String。

尝试替换此行:

h1 {
 line-height: 36px;
 font-size: 36px;
}

用这个:

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

您可能需要将for (int i=0 ;i< category.size();i++) { nameValuePairs.add(new BasicNameValuePair("category[]", category.get(i))); } key替换为"category",具体取决于您的服务器。