如果您使用http進行通信,最好使用HttpURLConnection。
JsonArray jsonArray;
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("http://www.example.com/endpoint").openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
httpURLConnection.connect();
InputStream is = httpsURLConnection.getInputStream();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
while (br.readLine() != null) {
sb.append(br.readLine()).append("\n");
}
br.close();
jsonArray = new JSONArray(sb.toString());
} catch (IOException | JSONException e) {
jsonArray = null;
}
也許你需要調用urlConnection.connect()之前的getInputStream() – BNK
什麼都沒有發生我的PHP文件是:打印(json_encode($輸出)); – charilaos13
嘗試檢查resp代碼,如果它是> = 400,getErrorStream而不是getInputStream – BNK