Java 类中的方法不能应用于给定类型;必需:HashMap<;字符串,整数>;

Java 类中的方法不能应用于给定类型;必需:HashMap<;字符串,整数>;,java,Java,我正在做这个程序,我得到了这个错误: 错误:(93,34)错误:类MainActivity.DownloadTask中的getPostDataString方法无法应用于给定类型; 必需:HashMap 找到:字符串,int 原因:实际参数列表和正式参数列表长度不同 protected String doInBackground(Object... params) { URL url; String response = "";

我正在做这个程序,我得到了这个错误:

错误:(93,34)错误:类MainActivity.DownloadTask中的getPostDataString方法无法应用于给定类型; 必需:HashMap 找到:字符串,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();
    }
private String getPostDataString(HashMap参数)引发UnsupportedEncodingException{
StringBuilder结果=新建StringBuilder();
布尔值优先=真;
对于(Map.Entry:params.entrySet()){
如果(第一)
第一个=假;
其他的
结果。追加(&);
append(URLEncoder.encode(entry.getKey(),“UTF-8”);
结果。追加(“=”);
result.append(URLEncoder.encode(String.valueOf(entry.getValue()),“UTF-8”);
}
返回result.toString();
}

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

替换以下语句

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

Map m=newhashmap();
m、 付诸表决(“地区”,1);
write(getPostDataString(m));

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

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;
}
受保护字符串doInBackground(对象…参数){
网址;
字符串响应=”;
试一试{
url=新url(“http://app.iseemobile.com/imenu/getDistrictRestaurants.php");
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
连接设置读取超时(15000);
连接设置连接超时(15000);
conn.setRequestMethod(“POST”);
conn.setDoInput(真);
连接设置输出(真);
OutputStream os=conn.getOutputStream();
BufferedWriter=新的BufferedWriter(
新的OutputStreamWriter(操作系统,“UTF-8”);
Map inputMap=newhashmap();
地图.put(地区),1 ;;
write(getPostDataString(map);
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if(responseCode==HttpsURLConnection.HTTP\u确定){
弦线;
BufferedReader br=新的BufferedReader(新的InputStreamReader(conn.getInputStream());
而((line=br.readLine())!=null){
响应+=行;
}
}否则{
答复=”;
}
}捕获(例外e){
e、 printStackTrace();
}
返回响应;
}

在调用
getPostDataString
时,您正在传递字符串和整数,因为它需要一个
HashMap
。Java不会直接将字符串和整数转换为映射。您需要在调用函数中创建映射并发送它
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;
}