2014-04-01 72 views
0


我正在嘗試從下面的REST API獲取響應。當我在瀏覽器中打開該URL時,我從服務器獲取了xml repsonse。但是當我試圖獲得相同的使用Java程序我得到異常無法在java中連接REST api

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


    } 


} 
+0

嘗試做的AsyncTask .. – Giridharan

回答

1

1)這個錯誤清楚地說:「can」 t解析主機名「。

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/

+0

嗨感謝您的答覆。但這是我正在運行的簡單的java程序。它不是特定於android的 – Anwar