无法从Android

时间:2017-08-05 16:38:07

标签: android amazon-web-services amazon-ec2 android-emulator

我在AWS EC2中托管了一项Web服务,可以使用公共DNS从Web浏览器访问该URL。当我通过从android Emulator运行它来访问它时,它抛出" java.net.UnknownHostException:无法解析主机Instance.us-east-2.compute.amazonaws.com/getJSON"

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");


}

1 个答案:

答案 0 :(得分:0)

我正在使用的客户端无法处理我使用的URL的响应,我使用AsynHttpClient来修复此。源链接:http://loopj.com/android-async-http/