Android套接字应用程序需要花费太多时间来加载

时间:2013-07-01 04:06:26

标签: java android sockets

我能够使用套接字成功连接到服务器。问题是活动需要花费太多时间来加载。我正在使用handler和runnable为我的网络类中的每一行更改TextView。看起来像everythhing已经完成然后我得到输出...我已经将网络视为一个线程,认为它将在后台运行,应用程序将立即加载。有什么建议可以吗?

主要活动

package com.abhishek.ally2;

import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView textview;
    String header;
    @SuppressLint("NewApi") @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        textview = (TextView)findViewById(R.id.status);
        header = "GET /ally.php HTTP/1.0\nHost: easyvote.co.in\n\n";
        Handler handler = new Handler();
        Thread connect = new network("easyvote.co.in", 80, header, textview, handler);
        connect.start();
        ((network) connect).statusShow();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

网络课程

package com.abhishek.ally2;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Handler;
import android.util.Log;
import android.widget.TextView;


public class network extends Thread {
    Socket client = null;
    DataOutputStream os = null;
    DataInputStream is = null;
    String host;
    int port;
    String responseLine;
    String data;
    String lastMsg;
    public boolean status;
    Handler handler;
    TextView txt;
    Runnable r;
    int count;
    network(String host, int port, String data, TextView status, Handler handler)
    {
        this.status = false;
        this.txt = status;
        this.handler = handler;
        this.host = host;
        this.port = port;
        this.data = data;
        count = 0;
        //while(!status)
        r = new Runnable(){

            @Override
            public void run() {
                //Log.d("Response", responseLine);
                txt.setText("Connecting...");

            }

        };
        this.handler.post(r);

    }
    @SuppressWarnings({ "deprecation" })
    public void statusShow()
    {
        try 
        {
            client = new Socket(host, port);
            os = new DataOutputStream(client.getOutputStream());
            is = new DataInputStream(client.getInputStream());
            if(client != null && os != null && is != null)
            {
                os.writeBytes(data);
                while((responseLine = is.readLine()) != null)
                {
                    lastMsg = responseLine;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Log.d("Response", responseLine);
                }
                os.close();
                is.close();
                client.close();
                Log.d("Response", lastMsg);
                r = new Runnable(){

                    @Override
                    public void run() {

                        txt.setText(lastMsg);

                    }

                };
                this.handler.post(r);

            }
        } 
        catch (UnknownHostException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

提前致谢:)

1 个答案:

答案 0 :(得分:2)

您正在使用statusShow()的{​​{1}}方法调用方法onCreate()

因此,该功能在UI /主线程上执行。尝试从MainActivity内部调用该方法。然后它不会阻止主线程。

编辑:这可能有用(我还没有测试过)

虽然下面的代码可能有用,但你做错了。你这样做的方式,甚至不需要单独的类(更不用说从Runnable r继承它了)。您可以在Thread完全编写Runnable r,在MainActivity编写。

主要活动:

post

在网络课程中:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    textview = (TextView)findViewById(R.id.status);
    header = "GET /ally.php HTTP/1.0\nHost: easyvote.co.in\n\n";
    Handler handler = new Handler();
    Thread connect = new network("easyvote.co.in", 80, header, textview, handler);
    connect.start();
}