我在做這個程序,我得到這個錯誤:類中的方法不能應用於給定的類型;必需的:HashMap中<字符串,整數>
錯誤:(93,34)誤差:類MainActivity.DownloadTask方法getPostDataString不能被應用到給定的類型; 要求:發現的HashMap :字符串,整數 原因:實際的和正式的參數列表長度
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();
}
你傳遞一個字符串和一個整數參數,而調用' getPostDataString',因爲它需要一個'HashMap'。 Java不會直接將字符串和整數轉換爲地圖。您需要在呼叫功能中創建地圖併發送 –