Reputation: 1
Please help me in my code below. It throws the error: Android socket: buffer is null. Can you tell me what is wrong?
try {
socket = new Socket("217.16.10.21", 1991);
out = new PrintStream(socket.getOutputStream(), true);
out.write(("285|SIP|0200|**4536780670138355*000000*1000***1309181650****000094*130918165044**1408***0918*****901301654144**300*************000000023834****00400013*65432100400013 ******P920035595*952*******************************************************************************").getBytes());
byte[] buffer = null;
int is=socket.getInputStream().read(buffer, 0, 1024);
socket.setSoTimeout(500);
String message=new String(buffer, 0, is);
//in = new BufferedReader(new InputStreamReader(is), 8192);
out.close();
//String message=in.readLine();
textaff.setText(message);
in.close();
//socket.close();
Toast.makeText(getApplicationContext(), " "+message, 2000).show();
} catch (UnknownHostException e) {
Toast.makeText(getApplicationContext(), "erreur"+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "erreur 2 "+e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Upvotes: 0
Views: 205
Reputation: 268
After out.write(("285|SIP|0200|**4536780670138355*000000*1000***1309181650****000094*130918165044**1408***0918*****901301654144**300*************000000023834****00400013*65432100400013 ******P920035595*952*******************************************************************************").getBytes());
Write:
socket.shutdownOutput();
Upvotes: 0
Reputation: 83
Use this listener to get incoming messages:
public Emitter.Listener handleIncomingMessages = new Emitter.Listener() {
@Override
public void call(final Object... args) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
String message = null;
String imageText;
try {
message = data.getString("message").toString();
Log.v("messageeeeeeeeeee",message);
} catch (JSONException e) {
// return;
}
addMessage(message);
try {
imageText = data.getString("image");
addImage(decodeImage(imageText));
} catch (JSONException e) {
//return
}
}
});
}
};
Upvotes: 0
Reputation: 40367
byte[] buffer = null;
int is=socket.getInputStream().read(buffer, 0, 1024);
Will not work, as you have not allocated a buffer for the .read() method to store results in - in fact you have explicitly set it to null.
You need to do something such as
byte[] buffer = new byte[1024];
to actually allocate your buffer.
Upvotes: 1