Question

I am new to this forum.

I am currently programming an android app that communicates with a java-based server. The aim is to convert photos in one fragment (made with cam) to a Base64-String and send them to the java-server. This is allready working and the communication between the server and the android app is working fine.

After that the server should send the Base64-String back to the client (to an other fragment). That is also working well.

The main problem is, when the client receives the String I am getting only a single line. I would like to append the lines to one String, but it does not work!

The main Aim is to get the whole photo back.

I would appreciate some help.

This is my Class which connects to Server and receives Strings from it. This Class runs extends an AsyncTask.

import android.os.AsyncTask;

public class ConnectToServer extends AsyncTask {

static Socket socket;
String encodedBase64;
int protocolId;
private static DataOutputStream DOS;
String result1;
String value;

public ConnectToServer(String encoded) {
    this.encodedBase64 = encoded;

}

public ConnectToServer(int i) {
    this.protocolId = i;
}

public ConnectToServer() {

}

protected String doInBackground(Void... arg0) {

    try {
        socket = new Socket("192.168.1.104", 17110);
        DOS = new DataOutputStream(socket.getOutputStream());

        if (protocolId == 1) {
            DOS.writeUTF("pictureload");
            protocolId = 0;

        } else {
            DOS.writeBytes(encodedBase64);
            receive();

        }

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result1;
}

public String receive() {

    if (socket.isConnected()) {

        try {
            BufferedReader input = new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));

            StringBuffer line = new StringBuffer();
            while ((result1 = input.readLine()) != null) {


                    result1 = input.readLine();
                    line.append(result1);

                }

            input.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return result1;
}

protected void onPostExecute(String result1) {
    TimelineActivity tA = new TimelineActivity();
    tA.encodeBase64(result1);
}

}

Was it helpful?

Solution

Editing my code once more. Ok, you are using global result1 variable, which is returned in doInBackground(), and changed in receive(), but in receive() its last value will be null at the end of reading from socket. Also, in your receive() function, you are building StringBuffer line, but at the end, you are returning String result1. All the modifications and comments in the full code below...

import android.os.AsyncTask;

public class ConnectToServer extends AsyncTask {
    static Socket socket;
    String encodedBase64;
    int protocolId;
    private static DataOutputStream DOS;

    String value;

    public ConnectToServer(String encoded) {
        this.encodedBase64 = encoded;
    }

    public ConnectToServer(int i) {
        this.protocolId = i;
    }

    public ConnectToServer() {

    }

    protected String doInBackground(Void... arg0) {
        //*****local variable
        String res = null;

        try {
            socket = new Socket("192.168.1.104", 17110);
            DOS = new DataOutputStream(socket.getOutputStream());

            if (protocolId == 1) {
                DOS.writeUTF("pictureload");
                protocolId = 0;
            } else {
                DOS.writeBytes(encodedBase64);

                //*****lets get the string returned by receive() method
                res = receive();
            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //*****and return it
        return res;
    }

    public String receive() {
        String receiveResult = null;

        if (socket.isConnected()) {

            try {
                BufferedReader input = new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));

                StringBuffer line = new StringBuffer();

                while ((receiveResult = input.readLine()) != null) {
                    line.append(receiveResult);
                }

                input.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //***** return your accumulated StringBuffer as string, not current line
        return line.toString();
    }

    protected void onPostExecute(String result1) {
        TimelineActivity tA = new TimelineActivity();
        tA.encodeBase64(result1);
    }
}

OTHER TIPS

you are reading the line two time in your while loop

Need to change

  while ((result1 = input.readLine()) != null) {
      result1 = input.readLine();
      line.append(result1);
  }

to

  while ((result1 = input.readLine()) != null) {         
      line.append(result1);
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top