1

我有一个通过套接字连接到服务器的类,由于某种原因,它总是从这里超时,我不知道为什么,起初我认为它与 onCreate() 有关,这就是为什么 doit () 甚至存在。任何帮助,将不胜感激。这是我的代码...

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

;
4

1 回答 1

1

恐怕这个问题缺少一些重要的细节(连接时会超时,对吗?),但我的盲目猜测是您的设备正在使用蜂窝连接,而 192.168.1.106 在您的 WiFi 网络上 - 192.168.xx 池中的 IP是私有 IP 地址,显然您无法通过 Internet 连接到任何此类 IP 地址。

但是你的代码还有另一个严重的问题——你试图onCreate()在应用程序的主线程中执行阻塞 I/O 调用。你不应该这样做(实际上,一旦你在 Android 3.x 或更高版本上尝试它,你就会得到NetworkOnMainThreadException)。网络 I/O 应该总是发生在另一个线程中,或者显式地,或者可能使用AsyncTask(它为你运行后台线程)。

有关好的介绍,请参阅这篇文章Designing for Responsiveness指南。

于 2012-10-11T22:12:33.477 回答