error未能链接服务器,NullPointerError:无法连接到服务器

我正在尝试连接到我的XAMPP服务器并使用以下类与MySQL数据库进行交互.但是,错误指出我在该行收到NullPointerException:

result = imService.createNewGroup(newGroupName);

在CreateGroup类中.应该注意的是,在用户将文本输入到对话框中并且从那里开始服务之后,也会立即调用CreateGroup类.我对服务和网络连接相当新,但有些东西我错过了应该允许在尝试发送.createGroup命令之前至少验证服务是否已连接?

CreateGroup类:

public class CreateGroup extends Activity {

private static final String SERVER_RES_RES_SIGN_UP_SUCCESFULL = "1";

private static final String SERVER_RES_SIGN_UP_USERNAME_CRASHED = "2";

private Manager imService;

private Handler handler = new Handler();

String newGroupName;

public ServiceConnection mConnection = new ServiceConnection() {

public void onServiceConnected(ComponentName className, IBinder service) {

imService = ((MessagingService.IMBinder) service).getService();

}

public void onServiceDisconnected(ComponentName className) {

imService = null;

Toast.makeText(CreateGroup.this, R.string.local_service_stopped,

Toast.LENGTH_SHORT).show();

}

};

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

bindService(new Intent(CreateGroup.this, MessagingService.class),

mConnection, Context.BIND_AUTO_CREATE);

// Getting intent and info from the dialog

Intent i = getIntent();

Bundle extras = i.getExtras();

newGroupName = extras.getString("groupName");

Thread thread = new Thread() {

String result = new String();

@Override

public void run() {

// Send group name to the messaging

// service

try {

result = imService.createNewGroup(newGroupName);

} catch (NullPointerException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Log.d("problem", "The value of result is " + result.toString());

handler.post(new Runnable() {

@Override

public void run() {

if (result == null) {

Toast.makeText(getApplicationContext(),

"It's null, not working", Toast.LENGTH_LONG)

.show();

}

if (result != null

&& result

.equals(SERVER_RES_RES_SIGN_UP_SUCCESFULL)) {

Toast.makeText(getApplicationContext(),

R.string.signup_successfull,

Toast.LENGTH_LONG).show();

// showDialog(SIGN_UP_SUCCESSFULL);

} else if (result != null

&& result

.equals(SERVER_RES_SIGN_UP_USERNAME_CRASHED)) {

Toast.makeText(getApplicationContext(),

R.string.signup_username_crashed,

Toast.LENGTH_LONG).show();

// showDialog(SIGN_UP_USERNAME_CRASHED);

} else // if

// (result.equals(SERVER_RES_SIGN_UP_FAILED))

{

Toast.makeText(getApplicationContext(),

R.string.signup_failed, Toast.LENGTH_LONG)

.show();

// showDialog(SIGN_UP_FAILED);

}

}

});

}

};

thread.start();

}

"createGroup"方法的服务器案例:

case "createGroup":

$SQLtest = "insert into groups(groupName, uniqueGroup, createTime)

VALUES('TestGroup', 1234567891, NOW())";

error_log("$SQLtest", 3 , "error_log");

if($result = $db -> query($SQLtest))

{

$out = SUCCESSFUL;

}

else

{

$out = FAILED;

}

break;

Messaging Service和createGroup方法:

public class MessagingService extends Service implements Manager, Updater {

// private NotificationManager mNM;

public static String USERNAME;

public static final String TAKE_MESSAGE = "Take_Message";

public static final String FRIEND_LIST_UPDATED = "Take Friend List";

public static final String MESSAGE_LIST_UPDATED = "Take Message List";

public ConnectivityManager conManager = null;

private final int UPDATE_TIME_PERIOD = 15000;

private String rawFriendList = new String();

private String rawMessageList = new String();

SocketerInterface socketOperator = new Socketer(this);

private final IBinder mBinder = new IMBinder();

private String username;

private String password;

private boolean authenticatedUser = false;

// timer to take the updated data from server

private Timer timer;

private StorageManipulater localstoragehandler;

private NotificationManager mNM;

public class IMBinder extends Binder {

public Manager getService() {

return MessagingService.this;

}

}

@Override

public void onCreate() {

mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

localstoragehandler = new StorageManipulater(this);

// Display a notification about us starting. We put an icon in the

// status bar.

// showNotification();

conManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

new StorageManipulater(this);

// Timer is used to take the friendList info every UPDATE_TIME_PERIOD;

timer = new Timer();

Thread thread = new Thread() {

@Override

public void run() {

Random random = new Random();

int tryCount = 0;

while (socketOperator.startListening(10000 + random

.nextInt(20000)) == 0) {

tryCount++;

if (tryCount > 10) {

// if it can't listen a port after trying 10 times, give

// up...

break;

}

}

}

};

thread.start();

}

@Override

public IBinder onBind(Intent intent) {

return mBinder;

}

@Override

public String createNewGroup(String groupName) throws NullPointerException, UnsupportedEncodingException {

String params = "action=createGroup";

String result = socketOperator.sendHttpRequest(params);

return result;

}

}

当我们使用SSH协议连接服务器时,有时候会遇到这样的问题:无法连接服务器,出现错误提示“network error: connection refused”。这种情况一般是由于以下原因所引起的。 1. SSH服务未启动或端口号不正确 SSH服务在Linux系统中是通过端口号进行通信的,由于端口号的限制,可能会出现SSH服务无法启动的情况,或者端口号被修改后导致无法访问SSH服务。此时需要确认SSH服务是否正常运行,以及正确的端口号是否被使用,可以使用命令“systemctl status sshd”查看服务状态。 2. 防火墙设置限制了SSH连接 服务器上的防火墙可能会限制SSH连接,在默认情况下,端口号22是被设置为SSH连接的端口,如果服务器的防火墙没有开放22端口,那么就无法成功连接。此时需要配置防火墙策略,如在CentOS系统上,可以使用命令“firewall-cmd --add-service=ssh --permanent”,并使用命令“firewall-cmd --reload”使配置生效。 3. SSH服务配置不正确 SSH服务的配置文件可能被错误地修改,导致SSH服务无法正常运行,比如服务端口号的设置、密钥认证的配置等等。此时需要重新检查SSH服务的配置文件,可以使用命令“vim /etc/ssh/sshd_config”进行修改。 总之,当我们遇到“network error: connection refused”错误提示时,需要进行逐一排查以上原因,找到问题所在并及时修复,才能确保成功连接服务器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值