我正在编写一个Android应用程序,以使用套接字连接到服务器。我已经搜索了代码示例,显然我做的与其他人一样,但是当创建套接字时,应用程序停止工作,我无法找到原因。
失败的行是:
package com.example.javi.androidclientapp;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainActivity extends AppCompatActivity {
private Socket socket;
String message = "";
InputStream inputStream;
BufferedReader bufread;
TextView msg;
Thread ClientThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText server_ip = (EditText) findViewById(R.id.ip);
final EditText server_port = (EditText) findViewById(R.id.port);
final Button button = (Button) findViewById(R.id.connect);
msg = (TextView) findViewById(R.id.msg);
ClientThread = new Thread(new ClientThread());
button.setOnClickListener(new Button.OnClickListener(){
public void onClick(View view){
String btxt = button.getText().toString();
if(btxt.equalsIgnoreCase("Connect")){
try {
InetAddress serverAddr = InetAddress.getByName(server_ip.getText().toString());
/***** The line below fails: ****/
socket = new Socket(serverAddr, Integer.parseInt(server_port.getText().toString()));
button.setText("Disconnect");
inputStream = socket.getInputStream();
bufread = new BufferedReader(new InputStreamReader(inputStream));
ClientThread.start();
} catch (UnknownHostException e1) {
e1.printStackTrace();
alertOneButton(e1.toString());
} catch (IOException e1) {
e1.printStackTrace();
alertOneButton(e1.toString());
}
}else{
try{
socket.close();
}catch (UnknownHostException e2){
e2.printStackTrace();
alertOneButton(e2.toString());
} catch (IOException e2) {
e2.printStackTrace();
alertOneButton(e2.toString());
}
button.setText("Connect");
}
}
});
}
class ClientThread implements Runnable {
@Override
public void run() {
while (socket.isConnected()) {
String s_tmp = "";
try {
s_tmp = bufread.readLine();
if(!s_tmp.isEmpty()){
message += s_tmp + "\n";
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
msg.setText(message);
}
});
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
message += "Something wrong! " + e.toString() + "\n";
alertOneButton(message);
}
}
}
}
/*
* AlertDialog with one action button.
*/
public void alertOneButton(String msg) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Dialog Simple")
.setMessage(msg.toString())
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
}
}
这是我的完整代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.javi.androidclientapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>
清单:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="WSentinel Data Receiver"
android:textStyle="bold" />
<EditText
android:id="@+id/ip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="172.30.200.110"
android:inputType="text"
/>
<EditText
android:id="@+id/port"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="8082"
android:inputType="text"
/>
<Button
android:id="@+id/connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
布局:
{{1}}
我完全迷失在这里,任何小费都将受到赞赏!
Thnks