2

我能够使用套接字成功连接到服务器。问题是活动需要太多时间来加载。我正在使用处理程序和可运行的网络类中的每一行更改 TextView。似乎一切都完成了,然后我得到了输出......我已经将网络作为一个线程,认为它将在后台运行并且应用程序将立即加载。有什么建议我能做什么?

主要活动

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

}

提前致谢 :)

4

1 回答 1

2

您正在statusShow()从 的onCreate()方法调用该方法MainActivity

因此,该函数在 UI/主线程上执行。尝试从Runnable r. 然后它不会阻塞主线程。

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

尽管下面的代码可能有效,但您做错了。你这样做的方式,你甚至不需要单独的类(更不用说子类了Thread)。你可以把它完全写Runnable r进去。MainActivitypost

主要活动:

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(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...");
            String result = statusShow();
            txt.setText(result);
        }

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

            return lastMsg;
        }
    } 
    catch (UnknownHostException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return "Error";
}
于 2013-07-01T04:41:52.397 回答