2013-08-27 112 views
-1

我想通過http和outputstream連接一個android手機到我的本地機器上運行的服務器的內容,但不成功..!通過http和outputstream連接一個android手機到服務器

這裏是我的代碼:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    try { 

     URL url = new URL("http://192.168.24.42:80/DebugServer/"); 
     HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();  
     InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 
     readStream(in); 
     urlConnection.disconnect(); 

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


private void readStream(InputStream in) { 

    try { 
     in.read(); 
     in.read(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
+0

你有問題嗎? – Simon

+0

你會得到什麼錯誤? –

+0

Arent你應該連接到端口8080? –

回答

0
URL url = new URL("http://192.168.24.42:80/DebugServer/"); 

是你的連接網址。將端口更改爲8080,因爲這是服務器偵聽的位置。

而且,你有沒有去你tomcatbin文件夾中(我假設在這裏),然後運行startup.BATstartup.SH文件,因爲沒有你的服務器沒有運行。

要檢查,如果你的服務器運行正常,嘗試http://localhost:8080
並請,張貼logcat的:)