类中的方法不能应用于给定的类型; required:HashMap <string,integer>

时间:2017-06-05 07:12:19

标签: java

我正在做这个程序而且我收到了这个错误:

错误:(93,34)错误:类MainActivity.DownloadTask中的方法getPostDataString不能应用于给定的类型; 必需:HashMap found:String,int 原因:实际和正式的参数列表长度不同

protected String doInBackground(Object... params) {
            URL url;
            String response = "";
            try {
                url = new URL("http://app.iseemobile.com/imenu/getDistrictRestaurants.php");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(15000);
                conn.setConnectTimeout(15000);
                conn.setRequestMethod("POST");
                conn.setDoInput(true);
                conn.setDoOutput(true);
                OutputStream os = conn.getOutputStream();
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(os, "UTF-8"));
                writer.write(getPostDataString("district", 1));
                writer.flush();
                writer.close();
                os.close();
                int responseCode = conn.getResponseCode();
                if (responseCode == HttpsURLConnection.HTTP_OK) {
                    String line;
                    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    while ((line = br.readLine()) != null) {
                        response += line;
                    }
                } else {
                    response = "";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return response;
    }

使用以下方法:

private String getPostDataString(HashMap<String, Integer> params) throws UnsupportedEncodingException{
        StringBuilder result = new StringBuilder();
        boolean first = true;
        for(Map.Entry<String, Integer> entry:params.entrySet()){
            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8"));
        }
        return result.toString();
    }

2 个答案:

答案 0 :(得分:0)

您在调用getPostDataString时传递字符串和整数,因为它需要HashMap<String, Integer>。 Java不会直接将字符串和整数转换为映射。您需要在调用函数中创建一个映射并发送它。

替换以下声明

 writer.write(getPostDataString("district", 1));

Map<String, Integer> m = new HashMap<String, Integer>();
m.put("district",1);
writer.write(getPostDataString(m));

答案 1 :(得分:0)

您应该将map作为参数传递给getPostDataString方法

protected String doInBackground(Object... params) {
        URL url;
        String response = "";
        try {
            url = new URL("http://app.iseemobile.com/imenu/getDistrictRestaurants.php");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            Map<String, Integer> inputMap = new HashMap<String, Integer>();
            map.put("district", 1);
            writer.write(getPostDataString(map);
            writer.flush();
            writer.close();
            os.close();
            int responseCode = conn.getResponseCode();
            if (responseCode == HttpsURLConnection.HTTP_OK) {
                String line;
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line = br.readLine()) != null) {
                    response += line;
                }
            } else {
                response = "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
}