我必须为大学实现一个项目,我想要实现一个连接到我的计算机上的本地服务器的Android应用程序。 我编写了Client和Server类,但是当我在模拟器上运行Client类时,出现错误:
03-23 13:04:47.850 3499-3499/? I/art: Not late-enabling -Xcheck:jni (already on)
03-23 13:04:47.859 3499-3499/? W/art: Unexpected CPU variant for X86 using defaults: x86
03-23 13:04:48.036 3499-3504/? E/art: Failed writing handshake bytes (-1 of 14): Broken pipe
03-23 13:04:48.036 3499-3504/? I/art: Debugger is no longer active
03-23 13:04:48.037 3499-3504/? I/art: Starting a blocking GC Instrumentation
03-23 13:04:48.259 3499-3499/? W/System: ClassLoader referenced unknown path: /data/app/com.flora.drawmyfriend-2/lib/x86
03-23 13:04:48.596 3499-3499/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
03-23 13:04:49.126 3499-3499/com.flora.drawmyfriend I/debug: start
03-23 13:04:49.130 3499-3522/com.flora.drawmyfriend I/debug: Attempting to connect to server
03-23 13:04:49.132 3499-3522/com.flora.drawmyfriend E/debug: Permission denied
03-23 13:04:49.137 3499-3522/com.flora.drawmyfriend E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.flora.drawmyfriend, PID: 3499
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:318)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.InputStream java.net.Socket.getInputStream()' on a null object reference
at com.flora.drawmyfriend.MainActivity$MySyncTask.doInBackground(MainActivity.java:45)
at com.flora.drawmyfriend.MainActivity$MySyncTask.doInBackground(MainActivity.java:31)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
03-23 13:04:49.175 3499-3499/com.flora.drawmyfriend I/InstantRun: Starting Instant Run Server for com.flora.drawmyfriend
我继续在所有论坛上回答我的问题,但我无法找到解决方案。 有人可能会告诉我我的代码有什么问题吗?
我的客户类代码是:
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainActivity extends AppCompatActivity {
private static final int portnumber = 50000;
private Socket socket=null;
Socket client = null;
DataOutputStream dataoutput = null;
DataInputStream datainput = null;
private static final String debugString ="debug";
class MySyncTask extends AsyncTask<Object, Object, Void> {
@Override
protected Void doInBackground(Object... params) {
try {
//Connecting
Log.i(debugString, "Attempting to connect to server");
client = new Socket("10.0.2.2", portnumber);
Log.i(debugString, "Connection established");
} catch (IOException e) {
Log.e(debugString, e.getMessage());
}
//Ouverture des flux
try {
InputStream input = client.getInputStream();
datainput = new DataInputStream(input);
OutputStream output = client.getOutputStream();
dataoutput = new DataOutputStream(output);
} catch (IOException e) {
System.err.println("Erreur ouverture des flux joueur");
System.exit(1);
}
return null;
}
protected void onPreExecute() {
Log.i(debugString,"start");
}
}
public MainActivity() throws UnknownHostException {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MySyncTask asyncTask=new MySyncTask ();
asyncTask.execute(1000);
}
public void sendMessage (View view)
{
EditText editText = (EditText) findViewById(R.id.editText);
String nom = null;
try {
//Send message to server
nom=editText.getText().toString();
dataoutput.writeUTF(nom);
} catch (IOException e) {
Log.e(debugString, e.getMessage());
}
}
}
和Eclipse上的服务器代码(似乎有效):
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
class Joueur {
Socket joueur = null;
DataOutputStream dataoutput = null;
DataInputStream datainput = null;
String name;
String mark;
// Constructeur : ouverture des flux
Joueur (Socket j) {
this.joueur = j;
try {
InputStream input = j.getInputStream();
this.datainput = new DataInputStream(input);
OutputStream output = j.getOutputStream();
this.dataoutput = new DataOutputStream(output);
} catch (IOException e) {
System.err.println("Erreur ouverture des flux serveur");
System.exit(1);
}
}
}
public class Main {
private static final int portnumber = 50000;
public static void main(String[] args)
{
ServerSocket serverSocket;
Socket socket=null;
try {
System.out.println("Server starting at port number " + portnumber);
serverSocket = new ServerSocket(portnumber);
//Client connecting
System.out.println("waiting for clients to connect.");
socket = serverSocket.accept();
System.out.println("A client has connected");
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
Joueur joueur= new Joueur(socket);
String nom=null;
try {
nom = joueur.datainput.readUTF();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(nom);
}
}
我在Android Manifest中进行了必要的修改。
提前感谢您的时间!!