2010-09-13 106 views
2

我真的不知道這是爲什麼Android的代碼不工作..這表明未知的主機異常:未知的主機異常的HTTP請求的JSON的Android

String requestTpServer = "www.google.com"; 
HttpClient client = new DefaultHttpClient(); 
HttpResponse response = null ; 

try { 

    HttpGet get = new HttpGet(requestToServer); 

    get.setHeader("Accept", "application/json"); 
    get.setHeader("content-type", "application/json"); 

    response = client.execute(getm); 
    Log.i("Hit Success... in get deals response", "going to rendernow"); 

    if (response != null) { 
    Log.i("responsecode---", ""+response.getStatusLine().getStatusCode()); 
    String jsonResponseFromServer = response.toString(); 
    Log.i("json Response From Server", jsonResponseFromServer); 
    } 
    else { 
    Log.i("blank ", "reply "); 
    } 
} catch (ClientProtocolException e) { 

    e.printStackTrace(); 
} catch (IOException e) { 

    e.printStackTrace(); 
} 

String res = response.toString(); 
return res; 

回答

2

只是爲了好奇:你嘗試用前綴您的網址「HTTP://」?

我用下面的幫助打開我的HTTP連接:

private InputStream openHttpConnection() throws IOException { 
    InputStream in = null; 
    int response = -1; 

    URL url = new URL("http://www.myserver.com/someurl"); 
    URLConnection conn = url.openConnection(); 

    if (!(conn instanceof HttpURLConnection)) 
     throw new IOException("Not an HTTP connection"); 

    try { 
     HttpURLConnection httpConn = (HttpURLConnection) conn; 
     httpConn.setAllowUserInteraction(false); 
     httpConn.setInstanceFollowRedirects(true); 
     httpConn.setRequestMethod("GET"); 
     httpConn.connect(); 

     response = httpConn.getResponseCode(); 
     if (response == HttpURLConnection.HTTP_OK) { 
      in = httpConn.getInputStream(); 
     } 
    } catch (Exception ex) { 
     throw new IOException("Error connecting"); 
    } 
    return in; 
} 

也許沒有什麼幫助。

0

我得到的異常,當我背後代理...... 所以,如果你是落後的代理,在代碼中可以聯繫管理員或給代理設置。