java - 连接到套接字会锁定 UI

标签 java android sockets

我正在构建(嗯,试图构建)一个简单的 usenet 新闻阅读器。下面的代码有效。从 SharedPreferences 获取用户名、主机、密码并连接到服务器并成功进行身份验证,但是它会锁定 UI,直到完成所有任务。

我该如何更改此代码以使其不会锁定 UI?

package com.webfoo.newz;

import java.io.IOException;
import java.net.SocketException;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import org.apache.commons.net.nntp.NNTPClient;

public class NewzActivity extends Activity {

TextView statusText;
String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings;
NNTPClient nntpClient;
int port;
String username;
String password;
String host;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.statusText = (TextView)findViewById(R.id.connectionStatusTextView);
    this.nntpClient = new NNTPClient();
    this.settings = getSharedPreferences(PREFS_NAME, 0);
}

public void openSettings(View button){
    Intent settingsIntent = new Intent( NewzActivity.this, SettingsActivity.class );
    startActivity( settingsIntent );
}

public void makeConnection(View button) {

    this.statusText.setText("Connecting...");       
    this.port = settings.getInt("UsenetPort", 563);
    this.host = settings.getString("UsenetHost", "");
    this.nntpClient.setDefaultPort( port );
    this.nntpClient.setDefaultTimeout( 9999 );
    // this.nntpClient.setConnectTimeout( 9999 );
    this.statusText.setText("Connecting to " + host );

    try {
        this.nntpClient.connect( host );
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    this.statusText.setText("Connected to " + host );

    if( nntpClient.isConnected() ){
        setAuthDetails();
    }else{
        this.statusText.setText("Failed to Connected to " + host );
    }

}

private void setAuthDetails() {

    this.username = settings.getString("UsenetUsername", "");
    this.password = settings.getString("UsenetPassword", "");

    try {
        nntpClient.authinfoUser(username);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        nntpClient.authinfoPass(password);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    statusText.setText("Authenticated as " + username );

}


}

最佳答案

查看 AsyncTask

关于java - 连接到套接字会锁定 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6872382/

相关文章:

java - Android:java.lang.IllegalArgumentException:错误对话线程 ID:15555215554

android - 适用于 Android 应用程序的 Google Cloud Messaging,同一应用程序的不同环境

c - C 套接字编程中的 recv() 不是阻塞的吗?

c++套接字接受,连接的客户端列表

Eclipse 无法找到 JRE

java - maven 无法解决依赖关系

java - 如何制作Java游戏开始画面?

android - 偏好未以编程方式显示在子屏幕中

java - 在一组文档上使用 ForkJoinPool

javascript - Node.js + Socket.io 如何从 PHP 接收数据