1
我想使用HTTP GET方法獲取數據。 但是回覆是錯誤的java.net.ProtocolException:意外的狀態行:<!DOCTYPE HTML PUBLIC「 - // W3C // DTD HTML 4.01 // EN」
java.net.ProtocolException: Unexpected status line: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
如何解決?
public JSONArray GetPost_sport(String token, String sport_type){
JSONArray jsonArray = null;
try {
URL url = new URL("http://o-two-sport.com/api/posts/?sport_type="+sport_type);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization","Token " +token);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Connection", "close");
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null)
responseStrBuilder.append(inputStr);
jsonArray = new JSONArray(responseStrBuilder.toString());
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return jsonArray;
}
HTTP響應不是JSON海峽,它由HTML返回錯誤信息。您需要確認當您請求的HTTP響應無效時,哪種類型的內容。 – neuo