簡體   English   中英

使用HttpConnection時出錯

[英]Error while using HttpConnection

我正在嘗試從網站解析XML文件。 我正在使用HTTP連接從網站獲取XML內容。 在下載內容時,我遇到了一些問題。 代碼如下。

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();
        }

    }
} }

這是我用來下載內容的代碼。 我得到的錯誤是

錯誤

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

請求該網址有效。 任何人都可以提出解決此問題的解決方案。 還有其他下載方式嗎?

我試過下面的代碼,它的工作:

撥打電話的方法:

 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();
        }
    }

解析響應的參考方法:

 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();
    }

如何調用此方法:

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

        thread.start();

如果有幫助,請嘗試此操作。

謝謝..!

雖然您可以通過計算機的網絡瀏覽器訪問該網站。 我也可以訪問該鏈接。 但這並不意味着您的Android手機可以解析域名。 請使用adb shell嘗試

ping api.androidhive.info

您很可能會看到負面的回應。

這是模擬器的問題。 在某些情況下,仿真器無法訪問網站,盡管可以通過我們的PC瀏覽器訪問該網站。 我在Genymotion內重新安裝了虛擬設備(在我的情況下是Nexus 4.3),現在一切正常,模擬器可以訪問所有網站。

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM