【发布时间】:2015-10-23 21:23:07
【问题描述】:
我不知道为什么,但是当我执行 GET 请求时收到 POST 响应,这是我的方法:
public String performGetBrandCall(String requestURL) {
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "Bearer " + MainActivity.TOKEN);
conn.setDoInput(true);
conn.setDoOutput(true);
int responseCode=conn.getResponseCode();
Log.d(TAG, "Response Code: " + responseCode);
if (responseCode == HttpsURLConnection.HTTP_OK || responseCode == HttpsURLConnection.HTTP_CREATED) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line=br.readLine()) != null) {
response+=line;
}
} else {
if (responseCode >= 400 && responseCode < 500){
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getErrorStream()));
while ((line=br.readLine()) != null) {
response+=line;
}
} else {
response="";
}
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
我正在使用 setRequestProperty 添加标头,这是我为请求设置的唯一“参数”,但我收到了发布响应,但我不知道为什么。有什么建议吗?
【问题讨论】:
标签: android post get httpurlconnection