1
我想從我的學校的webservices獲取數據。我在Eclipse中寫下代碼:Android HttpsConnection - 內部服務器錯誤
List<Predmet> getInfo(String osobniCislo) throws IOException
{
String target = "https://stag-ws.zcu.cz/ws/services/rest/rozvrhy/getRozvrhByStudent?osCislo="
+ "A12B0141P"
+ "&outputFormatEncoding=windows-1250";
URL url = new URL(target);
HttpsURLConnection https = (HttpsURLConnection)url.openConnection();
https.setConnectTimeout(5000); // 5s timeout
https.setRequestMethod("GET");
https.setInstanceFollowRedirects(true);
https.setUseCaches(false);
https.setDoOutput(true);
https.connect();
//Response cod
int responseCode = https.getResponseCode();
System.out.println(https.getResponseMessage());
}
此代碼在Eclipse中正常工作,我得到我想要的。但是當我將它複製到Android Studio時,我得到了500 - Server Internal Error
。我有privilige在清單:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
和其他網站工作正常。有人知道哪裏可能會出現問題嗎?
我從一開始就看到一個錯誤,那就是您正在嘗試GET請求,但調用了'setDoOutput(true)',它將您的請求設置爲POST。如果你想做一個GET請求,你不需要'setRequestMethod(「GET」)或'setDoOutput(true)'。 「HttpsURLConnection」類默認設置爲使用GET請求。 – NoChinDeluxe