1

I want to download multiple images from server. I open socket and download first image and when I want download second image in this socket, download not proceed, socket are closed. My code below...

public class ClientThread implements Runnable {

    public void run() {
        try {
            Log.d("ClientActivity", "C: Connecting...");
            SocketAddress sockaddr = new InetSocketAddress(serverIpAddress, SERVERPORT);
            socket = new Socket();
            socket.setKeepAlive(true);
            socket.connect(sockaddr, 5000);
            if (socket.isConnected()) {
                final DataInputStream input = new DataInputStream(socket.getInputStream());
                final PrintWriter output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));

                output.println("GET /way/images/profile/1231 HTTP/1.1");
                output.println("Host: 192.168.1.2");
                output.println("User-Agent: Java");
                output.println("Accept: */*");
                output.println("Connection: Keep-Alive");
                output.println("");
                output.flush();

                String line;
                File file = new File("/sdcard/aaa.png");
                if (!file.exists()) {
                    file.createNewFile();
                }
                FileOutputStream fileOut = new FileOutputStream(file);
                System.out.println("Getting first file");
                while ( (line = input.readLine()) != null ) {
                    System.out.println(line);
                    fileOut.write(line.getBytes());
                }
                System.out.println("First file finished");
                fileOut.flush();
                fileOut.close();

                output.println("GET /way/images/profile/1231 HTTP/1.1");
                output.println("Host: 192.168.1.2");
                output.println("User-Agent: Java");
                output.println("Accept: */*");
                output.println("Connection: Keep-Alive");
                output.println("");
                output.flush();

                file = new File("/sdcard/aaa1.png");
                if (!file.exists()) {
                    file.createNewFile();
                }
                fileOut = new FileOutputStream(file);
                System.out.println("Getting second file");
                while ( (line = input.readLine()) != null ) {
                    System.out.println(line);
                    fileOut.write(line.getBytes());
                }
                System.out.println("Second file finished");

                fileOut.flush();
                fileOut.close();
                input.close();
                output.close();
                socket.close();
            }
            Log.d("ClientActivity", "C: Closed.");
        } catch (Exception e) {
            Log.e("ClientActivity", "C: Error", e);
            connected = false;
        }
    }
}

1 Answer 1

0

You seem to be downloading image files using HTTP, in which case, it would be simpler and better to use HttpURLConnection.

Code sample for downloading of images using HttpURLConnection can be found in this blog post: Downloading image in Android

You may also read about Android's HTTP Clients in Android Developers Blog.

Sign up to request clarification or add additional context in comments.

4 Comments

I not use HttpURLConnection because, I not want every time open and close connection(in this process required many time, I want download files in one socket for saving of time).
Take a look at this question (and its answers) on SO: stackoverflow.com/q/8913085/1321873
Thanks, but this post cant help me
Just to be sure that you did not miss the point, HttpURLConnection may reuse the same underlying Socket for multiple request/response pairs as mentioned in the Performance section of the documentaion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.