0

I currently have a connection established to some middleware, through which I send SQL queries to a database.

I'm having some trouble with converting the DataInputStream to a useable format (Whether or not that is a String). I looked at another StackOverflow question, however this did not resolve my problem since using

in.readLine();

is 'deprecated', whatever that means. I need to be able to read the response from my middleware.

private class NetworkTask extends AsyncTask<String, Void, Integer> 
{
    protected Integer doInBackground(String... params)
    {
        Message message = networkHandler.obtainMessage();

        String ip = "example ip";
        int port = 1234;

        try 
        {
            InetAddress serverAddr = InetAddress.getByName(ip);
            Log.d("EventBooker", "C: Connecting...");
            Socket socket = new Socket(serverAddr, port);               

            DataInputStream in = new DataInputStream(socket.getInputStream());


            try 
            {
                Log.d("EventBooker", "C: Connected. Sending command.");
                PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                out.println(params[0]);
                networkHandler.sendMessage(message);
                Log.d("EventBooker", "C: Sent.");
            } 
            catch (Exception e) 
            {
                Log.e("EventBooker", "S: Error", e);
            }
            Log.d("EventBooker", "C: Reading...");



            Log.d("EventBooker", "C: Response read, closing.");
            socket.close();
            Log.d("Eventbooker", "C: Closed.");
        } 
        catch (Exception e)
        {
            Log.e("EventBooker", "C: Error", e);
        }
        return 1;
    }
}
2
  • Try this: stackoverflow.com/questions/309424/… Commented Apr 13, 2013 at 14:35
  • @Rotemmiz - I tried this earlier too, with no luck. Can IOUtils not be used in Android? Eclipse doesn't like me using IOUtils. Commented Apr 13, 2013 at 14:45

1 Answer 1

2

Convert your DataInputStream to a BufferedReaderStream like this:

BufferedReader d = new BufferedReader(new InputStreamReader(in));

Then you want to get your actual String, to do this you do something like this:

StringBuffer sb = new StringBuffer();
String s = "";

while((s = d.readLine()) != null) {
    sb.append(s);
}

String data = sb.toString();

//Use data for w/e

Easy and simple!

The reason we don't just append it to a already existing string is that Java Strings are immutable so each time the String object is recreated, creating performance issues. Therefore the StringBuffer!

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

4 Comments

I tried this earlier, but I got this response: 1) String cannot be resolved to a variable. 2) Syntax error on token "s", delete this token. 3) Type mismatch: Cannot convert from String to boolean. 4) Type mismatch: Cannot convert from boolean to String
Additionally, the only quick fix that Eclipse offers is to add another " != null" after the " != null" we already have.
Omg sorry... Check the revised version it's not another String on the s... my bad, had a glass of wine...
I know, I removed that myself however I'm still getting the same errors.

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.