我正在尝试创建一个简单的Android客户端 - 服务器应用程序,它将向服务器发送一个字符串。
客户端:
public class MainActivity extends ActionBarActivity {
EditText et;
Button btn;
private Socket client;
private PrintWriter printWriter;
private String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
s = et.getText().toString();
et.setText("");
try {
client = new Socket("192.168.1.14", 4444);
printWriter = new PrintWriter(client.getOutputStream(), true);
printWriter.write(s);
printWriter.flush();
printWriter.close();
client.close();
} catch (UnknownHostException ex) {
Toast.makeText(getApplicationContext(), "UnknownHostException: " + ex.getMessage(), Toast.LENGTH_LONG).show();
} catch (IOException ex) {
Toast.makeText(getApplicationContext(), "IOException: " + ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
}
服务器:
public class Main {
private static ServerSocket serverSocket;
private static Socket socket;
private static InputStreamReader inReader;
private static BufferedReader bufferedReader;
private static String s;
public static void main(String[] args) {
char port = 4444;
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
System.out.println("Could not listen on port " + port);
}
System.out.println("Server started. Listening on port" + port);
while (true) {
try {
socket = serverSocket.accept();
inReader = new InputStreamReader(socket.getInputStream());
bufferedReader = new BufferedReader(inReader);
s = bufferedReader.readLine();
System.out.println(s);
inReader.close();
socket.close();
} catch (IOException e) {
System.out.println("Problen in message reading!");
}
}
}
}
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.eyalcherevatzki.clientserver" >
<uses-feature android:name="android.hardware.type.watch" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
logcat的:
04-25 12:27:30.386 18000-18000/com.test.eyalcherevatzki.clientserver V/Zygote﹕ Switching descriptor 45 to /dev/null
04-25 12:27:30.386 18000-18000/com.test.eyalcherevatzki.clientserver V/Zygote﹕ Switching descriptor 9 to /dev/null
04-25 12:27:30.386 18000-18000/com.test.eyalcherevatzki.clientserver D/dalvikvm﹕ Late-enabling CheckJNI
04-25 12:27:30.436 18000-18000/com.test.eyalcherevatzki.clientserver D/ActivityThread﹕ handleBindApplication:com.test.eyalcherevatzki.clientserver
04-25 12:27:30.436 18000-18000/com.test.eyalcherevatzki.clientserver D/ActivityThread﹕ setTargetHeapUtilization:0.75
04-25 12:27:30.436 18000-18000/com.test.eyalcherevatzki.clientserver D/ActivityThread﹕ setTargetHeapMinFree:2097152
04-25 12:27:30.476 18000-18000/com.test.eyalcherevatzki.clientserver I/dalvikvm﹕ DexOpt: illegal method access (call Landroid/content/res/TypedArray;.<init> (Landroid/content/res/Resources;[I[II)V from Landroid/content/res/XResources$XTypedArray;)
04-25 12:27:30.476 18000-18000/com.test.eyalcherevatzki.clientserver I/dalvikvm﹕ Could not find method android.content.res.TypedArray.<init>, referenced from method android.content.res.XResources$XTypedArray.<init>
04-25 12:27:30.476 18000-18000/com.test.eyalcherevatzki.clientserver W/dalvikvm﹕ VFY: unable to resolve direct method 82: Landroid/content/res/TypedArray;.<init> (Landroid/content/res/Resources;[I[II)V
04-25 12:27:30.476 18000-18000/com.test.eyalcherevatzki.clientserver D/dalvikvm﹕ VFY: replacing opcode 0x70 at 0x0002
04-25 12:27:30.506 18000-18000/com.test.eyalcherevatzki.clientserver I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
04-25 12:27:30.506 18000-18000/com.test.eyalcherevatzki.clientserver W/dalvikvm﹕ VFY: unable to resolve virtual method 584: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
04-25 12:27:30.506 18000-18000/com.test.eyalcherevatzki.clientserver D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-25 12:27:30.506 18000-18000/com.test.eyalcherevatzki.clientserver I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
04-25 12:27:30.506 18000-18000/com.test.eyalcherevatzki.clientserver W/dalvikvm﹕ VFY: unable to resolve virtual method 606: Landroid/content/res/TypedArray;.getType (I)I
04-25 12:27:30.506 18000-18000/com.test.eyalcherevatzki.clientserver D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-25 12:27:30.546 18000-18000/com.test.eyalcherevatzki.clientserver I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
04-25 12:27:30.546 18000-18000/com.test.eyalcherevatzki.clientserver W/dalvikvm﹕ VFY: unable to resolve virtual method 547: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
04-25 12:27:30.546 18000-18000/com.test.eyalcherevatzki.clientserver D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-25 12:27:30.546 18000-18000/com.test.eyalcherevatzki.clientserver I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
04-25 12:27:30.546 18000-18000/com.test.eyalcherevatzki.clientserver W/dalvikvm﹕ VFY: unable to resolve virtual method 549: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
04-25 12:27:30.546 18000-18000/com.test.eyalcherevatzki.clientserver D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-25 12:27:30.646 18000-18000/com.test.eyalcherevatzki.clientserver I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LNX.LA.3.5.2_RB1.04.04.02.065.012_msm8974_LNX.LA.3.5.2_RB1__release_AU ()
OpenGL ES Shader Compiler Version: E031.24.00.13
Build Date: 03/28/14 Fri
Local Branch: mybranch2893136
Remote Branch: quic/LNX.LA.3.5.2_RB1
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_LNX.LA.3.5.2_RB1.04.04.02.065.012 + NOTHING
04-25 12:27:30.676 18000-18000/com.test.eyalcherevatzki.clientserver D/OpenGLRenderer﹕ Enabling debug mode 0
04-25 12:27:30.736 18000-18000/com.test.eyalcherevatzki.clientserver I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41a99038 time:135458162
04-25 12:27:30.736 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:27:30.736 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:27:30.736 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:27:30.736 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:27:36.166 18000-18000/com.test.eyalcherevatzki.clientserver W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
04-25 12:27:39.986 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:27:39.986 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:27:39.986 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:27:39.986 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:27:41.256 18000-18000/com.test.eyalcherevatzki.clientserver W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
04-25 12:27:44.976 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:27:44.986 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:27:44.986 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:27:44.986 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:28:24.376 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:28:24.376 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:28:27.236 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:28:27.236 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:28:27.256 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-25 12:28:27.256 18000-18000/com.test.eyalcherevatzki.clientserver E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
我还添加了INTERNET
权限
我实际上在DrJava上运行服务器,这可能是问题吗?
我收到错误:“IOException:socket failed:ACCESS(Permission denied)”。
答案 0 :(得分:0)
在setContentView()
下面写下此代码段 if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
或
class MyTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// Do your request
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
答案 1 :(得分:0)
当应用程序尝试在其主线程上执行网络操作时,将抛出异常 NetworkOnMainThreadException 。
因此,您需要在新的Thread or AsyncTask中执行网络操作。
以下是一个示例,
public void onClick(View v) {
s = et.getText().toString();
et.setText("");
/*run socket related code in new thread*/
new Thread(){
public void run() {
try {
client = new Socket("192.168.1.14", 4444);
printWriter = new PrintWriter(client.getOutputStream(), true);
printWriter.write(s);
printWriter.flush();
printWriter.close();
client.close();
} catch (UnknownHostException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}.start();
}
在示例代码printStackTrace()
中,如果发生错误,将打印日志跟踪。