我不知道为什么但在我做GET请求时我收到了回复,这是我的方法:
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添加标题,是唯一的&#34;参数&#34;我设定了请求,但我收到了帖子回复,我不知道为什么。有什么建议吗?
答案 0 :(得分:1)
根据Android开发者指南:
SetDoOutput 设置指示此URLConnection是否允许输出的标志。建立连接后无法设置。
只有在使用POST请求时才必须调用此方法...如果您正在执行GET请求且SetDoOutput为true,则您的请求将是POST请求。
我只删除该行,现在我得到了我的GET响应:
<input>