0


I am trying to get response from below REST api. when I open that URL in browser, I gets xml repsonse from server. but when I am trying to get the same using java program I am getting below exception

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)

Pls help me regarding this below is the code

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


    }


}
Anwar Shaikh
  • 104
  • 3
  • 5

1 Answers1

1

1) The error clearly says "can't resolve hostname".

2) Q: Does your AndroidManifest.xml grant Internet permissions? Make sure it contains this line:

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

3) Q: is this a real device, or an emulator? If the latter, consider recreating your AVD. Believe it or not, that can sometimes help: Android java.net.UnknownHostException: Host is unresolved.

4) Finally, here are some other tips - including proxy server configuration - that might help:

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

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190