Socket.isConnected()使我的Andr​​oid应用程序强制关闭 [英] Socket.isConnected() make my android app force close

查看:112
本文介绍了Socket.isConnected()使我的Andr​​oid应用程序强制关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我的源$ C ​​$ C有关套接字在Android中,当我使用的方法

I don't know what happen with my source code about Socket in Android, when I use method

.isConnected()

.isConnected()

我的应用程序总是强制关闭。在这里,我的源$ C ​​$ C

My app always force close. And here my source code

public class MyActivity extends Activity {
    private String IP;
    private int PORT;
    private Socket socket;
    private PrintWriter printWriter;
    private TextView text;

    private EditText fieldIp;
    private EditText fieldPort;


    private Button connect;
    private FrameLayout frameIP;

    private String message;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        frameIP = (FrameLayout)findViewById(R.id.frameIP);
        connect = (Button)findViewById(R.id.connect);
        fieldIp = (EditText)findViewById(R.id.ip);
        fieldPort = (EditText)findViewById(R.id.port);
        text = (TextView)findViewById(R.id.keterangan);
        connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IP = fieldIp.getText().toString();
                PORT = Integer.parseInt(fieldPort.getText().toString());
                SocketConnect socketConnect = new SocketConnect(IP,PORT);
                socketConnect.execute();
            }
        });
    }


    private class SocketConnect extends AsyncTask<Void, Void, Boolean> {

        String ip;
        int port;

        public SocketConnect(String a, int b){
            this.ip = a;
            this.port = b;
        }


        @Override
        protected Boolean doInBackground(Void... params) {
            try {
                socket = new Socket();
                socket.connect(new InetSocketAddress(ip,port));
                if(socket.isConnected())
                {
                    text.setText("Connected!");
                }
                else
                {
                    text.setText("Failed to connect!");
                }
            } catch (IOException e) {
                Log.e("MyActivity",e.getMessage());
            }
            finally {
                startActivity(new Intent(getApplicationContext(),ListViewText.class));
            }
            return null;
        }
    }
}

和我在AndroidManifest.xml中使用

And I use this in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

我希望你能帮助我的人:(

I hope you can help me guys :(

推荐答案

更改 doInBackground 方法如下...

@Override
protected Boolean doInBackground(Void... params) {

    boolean success = true;

    try {
        socket = new Socket();
        socket.connect(new InetSocketAddress(ip, port));
    } catch (Exception e) {
        success = false;
        Log.e("MyActivity", e.getMessage());
    }
    return success;
}

然后添加 onPostExecute 方法...

@Override
protected void onPostExecute(boolean result) {
    if(result) {
        text.setText("Connected!");
        startActivity(new Intent(MyActivity.this, ListViewText.class));
    }
    else {
        text.setText("Failed to connect!");
    }
}

这篇关于Socket.isConnected()使我的Andr​​oid应用程序强制关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆