Reputation: 739
I need to send to my server java program(it runs on PC) email address and password taken from android app, but when i click on login button server doesn't receive my message and app crash
this is android code:
public class MainActivity extends Activity {
private TextView tv;
private Button button;
private EditText editText1;
private EditText editText2;
Socket client;
private class myTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
client = new Socket("192.168.1.3", 4444); //connect to server
}catch (UnknownHostException e){
return "Host not found";
}catch (IOException e) {
return "Exception Caught";
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if ("Host not found".equalsIgnoreCase(result)){
Toast.makeText(getApplicationContext(), "Host not found" ,Toast.LENGTH_LONG).show();
}else if("Exception Caught".equalsIgnoreCase(result)){
Toast.makeText(getApplicationContext(), "Connection error" ,Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Connection established" ,Toast.LENGTH_LONG).show();
}
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.register);
button = (Button)findViewById(R.id.login);
new myTask().execute();
tv.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
startActivity(new Intent(MainActivity.this, Register.class));
}
});
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(client.getOutputStream());
oos.writeUTF("LOGIN");
String emailText = editText1.getText().toString();
oos.writeUTF(emailText);
oos.flush();
String passwordText = editText2.getText().toString();
oos.writeUTF(passwordText);
oos.flush();
client.close();
} catch (IOException ex) {
Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
server crash:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java:2293)
at java.io.ObjectInputStream$BlockDataInputStream.readBlockHeader(ObjectInputStream.java:2473)
at java.io.ObjectInputStream$BlockDataInputStream.refill(ObjectInputStream.java:2543)
at java.io.ObjectInputStream$BlockDataInputStream.read(ObjectInputStream.java:2615)
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:337)
at java.io.ObjectInputStream$BlockDataInputStream.readUnsignedShort(ObjectInputStream.java:2808)
at java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:2864)
at java.io.ObjectInputStream.readUTF(ObjectInputStream.java:1072)
android app logcat:
06-02 10:42:56.948: E/AndroidRuntime(5383): FATAL EXCEPTION: main
06-02 10:42:56.948: E/AndroidRuntime(5383): java.lang.NullPointerException
06-02 10:42:56.948: E/AndroidRuntime(5383): at com.example.social_network.MainActivity$2.onClick(MainActivity.java:106)
06-02 10:42:56.948: E/AndroidRuntime(5383): at android.view.View.performClick(View.java:4240)
06-02 10:42:56.948: E/AndroidRuntime(5383): at android.view.View$PerformClick.run(View.java:17721)
06-02 10:42:56.948: E/AndroidRuntime(5383): at android.os.Handler.handleCallback(Handler.java:730)
06-02 10:42:56.948: E/AndroidRuntime(5383): at android.os.Handler.dispatchMessage(Handler.java:92)
06-02 10:42:56.948: E/AndroidRuntime(5383): at android.os.Looper.loop(Looper.java:137)
06-02 10:42:56.948: E/AndroidRuntime(5383): at android.app.ActivityThread.main(ActivityThread.java:5103)
06-02 10:42:56.948: E/AndroidRuntime(5383): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 10:42:56.948: E/AndroidRuntime(5383): at java.lang.reflect.Method.invoke(Method.java:525)
06-02 10:42:56.948: E/AndroidRuntime(5383): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-02 10:42:56.948: E/AndroidRuntime(5383): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-02 10:42:56.948: E/AndroidRuntime(5383): at dalvik.system.NativeStart.main(Native Method)
i need to use ObjectOutputStream because server is set to receive this object..what am i wronging? thanks
Upvotes: 0
Views: 563
Reputation: 81539
while I'm not entirely sure why it crashes with a NullPointerException, I don't think the ObjectOutputStream was designed to send multiple objects on the same stream. Rather than using multiple Strings, I would recommend using a single class to hold the data.
Also, rather than using the default ObjectOutputStream, I'd use the KryoNet serialization library, because it saves you the effort of using AsyncTasks (it's asynchronous and listener-based by default), and makes this process much easier. It is also cross-platform between desktop and Android.
https://github.com/EsotericSoftware/kryonet
The way it works is that you must register the class you want to send in the Kryo object of the Server and the Client, and then you can use the sendTCP(object) command to send the data. Deserialization into object is automatic, see the site for more details. It's really simple to use! :)
Upvotes: 1