Java Android Send a data to server application never use a onPostExecute() - java

I have a problem with send a data to server, to connect with server I used a socket. To send a data to server I use a AsyncTask. And I have a problem with :
when a application send a data , my app don't see a finish this action, it never use onPostExecute().
This is my code :
public class MyClientTask extends AsyncTask<Void, Void, Void> {
String dstAddress;
int dstPort;
String response = "";
MyClientTask(String addr, int port) {
dstAddress = addr;
dstPort = port;
}
#Override
protected Void doInBackground(Void... arg0) {
// if (pingHost(1000)) {
socket = null;
try {
socket = new Socket(dstAddress, dstPort);
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
byte[] theByteArray = message.getBytes();
lengthMessage = (short) theByteArray.length;
outputStream.writeByte((byte) 0xB);
outputStream.writeByte((byte) 0xA);
outputStream.writeByte((byte) 0xA);
outputStream.writeByte((byte) 0xD);
outputStream.writeShort(lengthMessage);
outputStream.write(theByteArray);
outputStream.writeShort(width);
outputStream.writeShort(height);
outputStream.writeInt(lengthbmp);
outputStream.writeInt(lengthResizebmp);
outputStream.writeShort(11);
outputStream.write(imageInByte );
outputStream.write(imageResizeInByte);
outputStream.writeByte((byte) 0xB);
outputStream.writeByte((byte) 0xE);
outputStream.writeByte((byte) 0xE);
outputStream.writeByte((byte) 0xF);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int bytesRead;
InputStream inputStream = socket.getInputStream();
while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
outputStream.flush();
outputStream.close();
} catch (UnknownHostException e) {
e.printStackTrace();
isSuccsess = false;
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
e.printStackTrace();
Log.d("la", "nie udało sie");
isSuccsess = false;
response = "IOException: " + e.toString();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (socket != null) {
try {
socket.shutdownInput();
socket.shutdownOutput();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(isSuccsess){
Toast.makeText(MainActivity.this, "Zdjęcie zostało wysłane !" , Toast.LENGTH_LONG).show();
bm = null;
clearEt();
ivImage.setImageBitmap(bm);
}
else{
Toast.makeText(MainActivity.this , "Nie udało się wysłać zdjęcia !" , Toast.LENGTH_LONG).show();
}
pbWheel.setVisibility(View.INVISIBLE);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
pbWheel.setVisibility(View.VISIBLE);
}
}

You can use AsyncTask<Void,Void,Integer>, and return any integer instead of a null in the doInBackground.
Just replace (in the doInBackground)
return null;
with
return 1;
and replace (in the AsyncTask declaration)
public class MyClientTask extends AsyncTask<Void, Void, Void> {
with
public class MyClientTask extends AsyncTask<Void, Void, Integer> {

Note that getInputStream() is a blocking call. It will block, until it receives data from server. In your case, you are not flushing data to server before calling getInputStream(), which will wait for response from server. Now you haven't sent any data to server (which I guess you should in your case to get response), socket will block the thread waiting for input.
You should flush data before getting input stream and making your thread sleep. Also close all streams once you are done, as closing stream will close underlying socket. From the docs:
Closing the returned OutputStream will close the associated socket.
Closing the returned InputStream will close the associated socket.
outputStream.flush(); // flush it before sleep and don't close
// outputStream.close(); don't close it here
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int bytesRead;
InputStream inputStream = socket.getInputStream();
while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}

Is you execution going after this lines?
while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
Use URLConnection instead of socket inside AsyncTask.

Related

Java socket server cannot receive many data at the same time, how to make java socket server accept data at the same time?

my java socket server cannot accept more than one data at the same time in one client
Thread t1 = new Thread(() -> {
ServerSocket ss = null;
try {
ss = new ServerSocket(9000);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(true) {
System.out.println("Waiting Transaction ..");
Socket clientSocket = ss.accept();
InetAddress inet = clientSocket.getInetAddress();
try{
while (clientSocket.getInputStream().available() == 0) {
Thread.sleep(100L);
}
byte[] data;
int bytes;
data = new byte[clientSocket.getInputStream().available()];
bytes = clientSocket.getInputStream().read(data,0,data.length);
String dataDB = new String(data, 0, bytes, "UTF-8");
System.out.println("received data\n time : "+ new Date() +"length data : " + dataDB.length());
System.out.println(dataDB);
String dataFrom = getFromServer(dataDB);
clientSocket.getOutputStream().write(dataFrom.getBytes("UTF-8"));
}catch (BindException be){
be.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally {
clientSocket.close();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
t1.start();
i try using thread but it not working, this code only accept the first data otherwise anoter data will be decline. how to server accept many data at the same time?
Let me tell you how I did it, while playing around with sockets, so I created a SocketServer class
public class Server {
public static final Integer port = 9000;
private final ServerSocket server;
private final ExecutorService service = Executors.newFixedThreadPool(10);
private final AtomicInteger idGenerator = new AtomicInteger(0);
public Server() throws IOException {
this.server = new ServerSocket(port);
}
public void start() {
try {
while (true) {
Worker worker = new Worker(idGenerator.incrementAndGet(), server.accept());
service.execute(worker);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Now every time, a client joins, I create a worker thread and submit it to ExecutorService (Executor service has pool of threads and run the passed worker by allocating a thread to it, you can read about it).
The worker class looks like this
public class Worker implements Runnable {
private final Socket client;
private final Logger log = LoggerFactory.getLogger(this.getClass());
public Worker(Integer id, Socket client) {
this.id = id; //Ignore it, it was for logging purpose, how many joined
this.client = client;
}
#Override
public void run() {
listenClientMessages();
closeConnection();
}
private void listenClientMessages() {
final int MAX_INPUT = 1024;
int read;
try (InputStream is = client.getInputStream()) {
byte[] buf = new byte[MAX_INPUT];
while ((read = is.read(buf)) != -1) {
String line = new String(buf, 0, read);
log.info(line);
if (line.equals("bye")) break;
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void closeConnection() {
try{
client.close();
}catch (IOException ex) {
ex.printStackTrace();
}
}
}
So, you can see, I am reading bytes in while look
byte[] buf = new byte[MAX_INPUT];
while ((read = is.read(buf)) != -1) {
String line = new String(buf, 0, read);
log.info(line);
if (line.equals("bye")) break;
}
that's why when MAX_INPUT or less is processed, it would wait from client to send next input.
Let me know if it helps.
Edit: As commented by #user207421 in comments, closeConnection function is redundant as inputstream is closed with try-wit-resources block above in listenClientMessages function, so it is not needed.

Transfer file from android to server via socket

There is such a problem. To transfer files in an Android application, we use a Socket, we encrypt and transfer to the server, there the data is decrypted and transmitted by the usual POST method, but when some data is transferred, the file is not loaded, but returns 500 The error stream terminated unexpectedly. One xlsx file is transferred. but the other is gone. We also saw that if the file is still uploaded to the server and we download it, the file is broken and at the end a piece of header Content-Disposition: form-data; name = "publicAccess" false.
ANDROID CODE
#Override
public void run() {
try {
byte[] request = new byte[4096];
byte[] reply = new byte[8192];
final InputStream inFromClient = sClient.getInputStream();
final OutputStream outToClient = sClient.getOutputStream();
SSLSocket remoteSocket = tlsConnectionService.createSSLSocket(remoteHost, remotePort);
final InputStream inFromServer = remoteSocket.getInputStream();
final OutputStream outToServer = remoteSocket.getOutputStream();
// a new thread for uploading to the server
new Thread() {
public void run() {
int bytes_read;
try {
while ((bytes_read = inFromClient.read(request)) != -1 ) {
String newReq = new String(request);
if (newReq != null) {
outToServer.write(newReq.replace(LOCAL_SOCKET_URL, remoteHost).getBytes(), 0, bytes_read);
outToServer.flush();
}
}
} catch (IOException e) {
if (!(e instanceof SocketException)) {
Log.e(M.CPP, e.toString());
}
}
}
}.start();
// current thread manages streams from server to client (DOWNLOAD)
int bytes_read;
try {
while ((bytes_read = inFromServer.read(reply)) != -1 ) {
outToClient.write(reply, 0, bytes_read);
outToClient.flush();
}
} catch (IOException e) {
Log.e(M.CPP, e.toString());
} finally {
try {
remoteSocket.close();
} catch (IOException e) {
Log.e(M.CPP, e.toString());
}
}
sClient.close();
} catch (IOException e) {
Log.e(M.CPP, e.toString());
}
}
Server CODE
#PostMapping(
value = {"/file", "/file/" },
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE
)
public ResponseEntity<FileEntryDto> upload(
#PathVariable("source") String source,
#AuthenticationPrincipal JwtUserDetails currentUser,
#Validated FileUploadRequest request
) {
return ResponseEntity.ok(
converters.fileToDto(
fileOperationService.upload(Source.fromName(source), request, currentUser.getUserId()))
);
}
The request that we see when inspecting app
request

Cannot send byte Data from Android App to Python Server using sockets

I have tried everything to get this to work. Basically I have an Android App which receives data from a Python based Server on a local network connection. I can receive Data no problem. However when I attempt to send data back the App crashes and the Python server receives blank data. I have tried several different approaches but non have worked. Here is the Python method I have written to receive the message:
def checkReply(self):
reply = "no reply yet"
self.conn.settimeout(1)
try:
test = self.conn.recv(1024)
except:
self.conn.timeout;
print("I failed to hear this") #Debug to help see if I have heard an incomming message
try:
data = test.decode()
reply = data
except:
print("I failed to decode this") #Debug to help see if I could not decode an incomming message
print(reply)
self.conn.settimeout(0)
My client on my Android application looks like this:
public class Client extends AsyncTask<Void, Void, Void> {
String dstAddress;
int dstPort;
String response = "No data has been sent yet";
TextView textResponse;
Socket socket = null;
Client(String addr, int port, TextView textResponse) {
dstAddress = addr;
dstPort = port;
this.textResponse = textResponse;
}
#Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
1024);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream inputStream = socket.getInputStream();
/*
* notice: inputStream.read() will block if no data return
*/
while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
response = byteArrayOutputStream.toString("Ascii");
}
byteArrayOutputStream.flush();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "IOException: " + e.toString();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
#Override
protected void onPostExecute(Void result) {
textResponse.setText(response);
super.onPostExecute(result);
}
protected String getSite(){
return response;
}
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void returnMsg(){
try (DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream())) {
byte[] buf = "hello".getBytes("UTF-8");
outToClient.writeBytes("Test");
outToClient.flush();
} catch (IOException e) {}
}
}
I am having to test on a physical device so I have no log to trace the error message. Any help would be greatly appreciated. Thanks
I managed to solve the issue. I needed to flush the buffer before trying to send any data back via the socket

Android App crashes on while()

In my android app I'm creating a Socket and a BufferedReader. If i just read one line of the BufferedReader I'm getting the response from the server. But if I'm trying it with while((message = br.readLine()) != null) The application crashes. Any ideas what could be the problem?
Code
public void connecting(String uid) {
uuid = uid;
try {
client = new Socket("127.0.0.1", 8234);
try {
tv = (TextView) chat.findViewById(R.id.textView);
pw = new PrintWriter(client.getOutputStream());
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
new Thread(new Runnable() {
#Override
public void run() {
try {
// If i just try this, it works
tv.append("\n"+br.readLine());
// If I'm trying this, it crashes
String message = "";
while((message = br.readLine()) != null) {
tv.append("\n"+message);
}
} catch (IOException e) {
tv.append(e.getMessage());
}
}
}).start();
tv.append("Connected!");
pw.println(uuid+":B31TR1TT");
pw.flush();
} catch (IOException e) {
tv.append(e.getMessage());
}
} catch (IOException e) {
tv.append(e.getMessage());
}
}
As algui91 said move the network calls to a separate thread. that strict mode error (violation=4) indicates network calls on ui thread.
Just refactor the network calls into a background task (service, asynctask , or whaetever ) , and the issue should go away.
Separate the UI from the business logic or network communication. Its always better and easier to test/debug.
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream inputStream = socket.getInputStream();
while ((bytesRead = inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
Instead of while loop code try to use this code
or visit this link http://hastebin.com/inecerakes.java

Android:socket communication

I am trying to create simple app with android client and java server
android client is able to send message to server(java)
while when i try to read server reply
error:socket closed.
line(if((receiveMessage = receiveRead.readLine()) != null) )
public class ClientConnectorTask extends AsyncTask<String, Void, Integer> {
private Socket client;
private PrintWriter printwriter;
protected Integer doInBackground(String...strings) {
// validate input parameters
if (strings.length <= 0) {
return 0;
}
// connect to the server and send the message
try {
client = new Socket("192.168.1.4", 7777);
printwriter = new PrintWriter(client.getOutputStream(),true);
//while(true){
InputStream istream = client.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
String receiveMessage;
while (true){
// printwriter.write(strings[0]);
printwriter.print(strings[0]);
printwriter.flush();
printwriter.close();
if((receiveMessage = receiveRead.readLine()) != null) //receive from server
{
System.out.println(receiveMessage); // displaying at DOS prompt
}
}
//}
//client.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
protected void onPostExecute(Long result) {
return;
}
}
Closing the PrintWriter inside the loop doesn't make sense, and closing it before the readLine() call doesn't make sense either. Closing either the input or the output stream of a Socket closes the other stream and the socket.

Categories