1
我在AWS EC2中託管了Web服務,並且可以使用公共DNS從Web瀏覽器訪問URL。當我通過從android模擬器運行它來訪問它時,它拋出「java.net.UnknownHostException:無法解析主機Instance.us-east-2.compute.amazonaws.com/getJSON」無法從Android訪問Amazon EC2實例
i)我已經給予權限適用於Android Manifest的INTERNET。 ii)我在EC2 的安全組中有一個開放的網關權限0.0.0.0:0我在哪裏出錯?
@Override
protected String doInBackground(String... params) {
try {
// Enter URL address where your json file resides
// Even you can make call to php file which returns json data
url = new URL("http://Instance.us-east-2.compute.amazonaws.com/getJSON");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
try {
// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("GET");
// setDoOutput to true as we recieve data from json file
conn.setDoOutput(true);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("Printing stack "+e1.toString());
return e1.toString();
}
try {
int response_code = conn.getResponseCode();
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return (result.toString());
} else {
return ("unsuccessful");
}
您的網址似乎並沒有被訪問。這似乎是一個獲取URL,但我仍然無法正確訪問它。查看您的網絡服務是否正確託管,並且您已經完成了當前的防火牆設置 –
將您的代碼中的真實網址複製並粘貼到瀏覽器中,並確保它可以訪問。很可能你在那裏有錯誤。 – Putnik
感謝您的答覆。 @kapsym,我在這裏提到的URL不是真正的。我出於安全原因修改了它。但是,當我從Web瀏覽器訪問它時,我可以得到正確的響應。 –