无法在java中连接REST api

时间:2014-04-01 06:08:07

标签: java


我想从REST api下面得到回应。当我在浏览器中打开该URL时,我从服务器获取xml repsonse。但是当我试图使用java程序获得相同的东西时,我会遇到异常

IOEXCeption
java.net.UnknownHostException: rxnav.nlm.nih.gov
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.<init>(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at com.test.demo.RestConnector.run(RestConnector.java:22)
    at java.lang.Thread.run(Unknown Source)

请帮我解决这个问题 下面是代码

import java.io.BufferedReader; 
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class RestConnector implements Runnable{

    @Override
    public void run() {
        String URLString = "http://rxnav.nlm.nih.gov/REST/classes?src=MESH";

        try {
            URL url = new URL(URLString);
            HttpURLConnection connection  =  (HttpURLConnection)url.openConnection();

            connection.setRequestMethod("GET");
            connection.setRequestProperty("Accept", "application/xml");
            if(connection.getResponseCode() != 200){
                System.out.println("Failed to connect:"+connection.getResponseCode());
                System.out.println("Response Message:"+connection.getResponseMessage());
            }

            System.out.println("I am here ");
            BufferedReader  reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            String line ; 
            while((line = reader.readLine()) != null){
                System.out.println(line);
            }

            connection.disconnect();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("IOEXCeption");
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {

        Thread t = new Thread(new RestConnector());
        t.start();


    }


}

1 个答案:

答案 0 :(得分:1)

1)错误明确表示“无法解析主机名”。

2)问:您的AndroidManifest.xml是否授予Internet权限?确保它包含以下行:

<uses-permission android:name="android.permission.INTERNET" />

3)问:这是真正的设备还是模拟器?如果是后者,请考虑重新创建AVD。信不信由你,有时可以提供帮助:Android java.net.UnknownHostException: Host is unresolved

4)最后,这里有一些其他提示 - 包括代理服务器配置 - 可能有所帮助:

http://eliasbland.wordpress.com/2011/02/22/java-net-unknownhostexception-on-android-a-list-of-possible-causes/