简体   繁体   English

使用HttpConnection时出错

[英]Error while using HttpConnection

QUESTION

I am trying to parse an XML file from a website. 我正在尝试从网站解析XML文件。 I am using an HTTP connection for getting the XML content from the website. 我正在使用HTTP连接从网站获取XML内容。 While I am downloading the content I am facing some issues. 在下载内容时,我遇到了一些问题。 The code is given below. 代码如下。

CODE

public class MainActivity extends AppCompatActivity {
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button= (Button) findViewById(R.id.button);

}
public void process(View view)  {
    Thread thread=new Thread(new Download());
    thread.start();



}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
public class Download implements Runnable {
    public void run() {
        try {
            URL url = new URL("http://api.androidhive.info/pizza/?format=xml");
            HttpURLConnection connection= (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.getDoOutput();
            InputStream stream = connection.getInputStream();
            Toast.makeText(MainActivity.this, stream + "", Toast.LENGTH_LONG).show();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
} }

This is the code I am using to download the content. 这是我用来下载内容的代码。 The error I am getting is 我得到的错误是

ERROR 错误

10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ java.net.UnknownHostException: Unable to resolve host "api.androidhive.info": No address associated with hostname
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at com.example.sajithm.domparsing.MainActivity$Download.run(MainActivity.java:65)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.io.Posix.getaddrinfo(Native Method)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
10-21 01:14:21.459  25125-25181/com.example.sajithm.domparsing W/System.err﹕ ... 15 more
    --------- beginning of /dev/log/system
10-21 01:19:34.259  25125-25131/com.example.sajithm.domparsing D/dalvikvm﹕ GC_FOR_ALLOC freed 306K, 4% free 9003K/9336K, paused 5ms, total 5ms

REQUEST The web address is valid. 请求该网址有效。 Can anyone please suggest a solution to tackle this problem. 任何人都可以提出解决此问题的解决方案。 Is there any other way to download the content? 还有其他下载方式吗?

I have tried below code and it worked : 我试过下面的代码,它的工作:

Method to make a call : 拨打电话的方法:

 public void makeXMLRequest() {
        try {
            URL url = new URL("http://api.androidhive.info/pizza/?format=xml");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            con.getDoOutput();
            String readStream = readStream(con.getInputStream());
            // Give output for the command line
            System.out.println(readStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Reference method to parse response : 解析响应的参考方法:

 private static String readStream(InputStream in) {
        StringBuilder sb = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(in));) {

            String nextLine = "";
            while ((nextLine = reader.readLine()) != null) {
                sb.append(nextLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

How to call this method : 如何调用此方法:

 Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                makeXMLRequest();
            }
        });

        thread.start();

Try this if it can help. 如果有帮助,请尝试此操作。

Thanks..! 谢谢..!

Though you can access the website by web browser of your computer. 虽然您可以通过计算机的网络浏览器访问该网站。 I can access the link as well. 我也可以访问该链接。 But it doesn't mean your Android phone can resolve the domain name. 但这并不意味着您的Android手机可以解析域名。 Please use adb shell to try 请使用adb shell尝试

ping api.androidhive.info

You will most probably see a negative response. 您很可能会看到负面的回应。

This is a problem with emulator. 这是模拟器的问题。 In some cases emulator cannot access websites though the site is accessible through our pc browser. 在某些情况下,仿真器无法访问网站,尽管可以通过我们的PC浏览器访问该网站。 I reinstalled the virtual device (In my case it was Nexus 4.3) inside Genymotion,now everything works fine and the emulator is capable to access all websites. 我在Genymotion内重新安装了虚拟设备(在我的情况下是Nexus 4.3),现在一切正常,模拟器可以访问所有网站。

Thank you 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM