2012-10-11 127 views
1

我有這個類,它通過套接字連接到服務器,出於某種原因,它總是從這裏超時,我不明白爲什麼,我首先想到它與它有關onCreate(),這就是爲什麼doit()甚至存在。任何幫助,將不勝感激。這裏是我的代碼...Android套接字總是超時

public class Ads extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_ads); 
     doit(); 
    }; 
    public void doit(){ 
     Socket socket = null; 
     FileOutputStream fos = null; 
     DataInputStream dis = null; 
     BufferedOutputStream buf = null; 
     DataOutputStream dos = null; 

     try { 
      socket = new Socket("192.168.1.106", 4447); 
      Bundle extras = getIntent().getExtras(); 

      String value = extras.getString("keyName"); 

      dos = new DataOutputStream(
        new BufferedOutputStream(socket.getOutputStream())); 
      dis = new DataInputStream(new BufferedInputStream(
        socket.getInputStream())); 
      //dos.writeChars(value); 
      int numFiles = dis.readInt(); 
      File sdCard = Environment.getExternalStorageDirectory(); 
      File dir = new File (sdCard.getAbsolutePath() +value); 
      dir.mkdirs(); 
      if (dir.isDirectory()) { 
       String[] children = dir.list(); 
       for (int i=0; i<children.length; i++) { 
        new File(dir, children[i]).delete(); 
       } 
      } 
      int n = 0; 
      int fileLength = 0; 
      for (int i=0;i<numFiles;i++){ 
       File file = new File(dir, String.valueOf(i)+".png"); 
       Log.d("debug tag","created file "+file); 
      } 
      for (int i=0;i<numFiles;i++){ 
       fileLength = dis.readInt(); 


       byte[] temp = new byte[(int) fileLength]; 

       String path = sdCard.getAbsolutePath()+value+"/"+i+".png"; 
       buf = new BufferedOutputStream(new FileOutputStream(path)); 
       while ((fileLength > 0) && (n = dis.read(temp, 0, (int) Math.min(temp.length, fileLength))) != -1) { 
        buf.write(temp,0,n); 
        buf.flush(); 
        fileLength -= n; 
       } 
       //buf.close(); 

      Log.d("debug tag","the file is "+temp.length+" bytes long"); 
      } 
      // now read in text files 

      n = 0; 
      fileLength = 0; 
      for (int i=0;i<numFiles;i++){ 
       File file = new File(dir, String.valueOf(i)+".txt"); 
       Log.d("debug tag","created file "+file); 
      } 
      for (int i=0;i<numFiles;i++){ 
       fileLength = dis.readInt(); 


       byte[] temp = new byte[(int) fileLength]; 

       String path = sdCard.getAbsolutePath()+value+"/"+i+".txt"; 
       buf = new BufferedOutputStream(new FileOutputStream(path)); 
       while ((fileLength > 0) && (n = dis.read(temp, 0, (int) Math.min(temp.length, fileLength))) != -1) { 
        buf.write(temp,0,n); 
        buf.flush(); 
        fileLength -= n; 
       } 
       //buf.close(); 

      Log.d("debug tag","the text file is "+temp.length+" bytes long"); 
      } 
      generateListView(sdCard.getAbsoluteFile()+value+"/"); 
     } catch (UnknownHostException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     finally{ 
      if (socket != null){ 
       try { 
        socket.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 

      if (fos != null){ 
       try { 
        fos.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 

      if (dis != null){ 
       try { 
        dis.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
      if (dos != null){ 
       try { 
        dos.close(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 

; 

回答

1

恐怕這個問題缺少一些重要的細節(它連接時超時,對吧?),但我盲目的猜測是,您的設備使用蜂窩連接,而192.168。 1.106在您的WiFi網絡上 - 192.168.xx池中的IP爲private IP addresses,顯然,您無法通過Internet連接到任何此類IP地址。

但是,您的代碼還存在另一個嚴重問題 - 您正試圖阻止I/O調用onCreate() - 這是在應用程序的主線程中執行的。你不應該這樣做(實際上,只要你在Android 3.x或更高版本上試用它,你就會得到NetworkOnMainThreadException)。網絡I/O應該總是在另一個線程中發生,或者明確地,或者使用AsyncTask(它爲您運行後臺線程)。

對於一個很好的介紹,請參閱this postDesigning for Responsiveness指南。

+0

是的我知道私人IP地址,目前正在測試我的本地網絡。我有一種感覺,它與它在onCreate()之下有關係,所以謝謝你幫助確認 – user1661396

+0

好吧,在onCreate()中的網絡顯然是一件壞事,但它並不能真正解釋連接時的超時。用於測試的Android版本是什麼? –

+0

Android版本2.2 – user1661396