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;
    }
}
Community
  • 1
  • 1
Boggy B
  • 171
  • 1
  • 8
  • Try this: http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string?rq=1 – Rotemmiz Apr 13 '13 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. – Boggy B Apr 13 '13 at 14:45

1 Answers1

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!

CJBS
  • 13,354
  • 5
  • 76
  • 124
Johan S
  • 3,261
  • 4
  • 31
  • 56
  • 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 – Boggy B Apr 13 '13 at 14:43
  • Additionally, the only quick fix that Eclipse offers is to add another " != null" after the " != null" we already have. – Boggy B Apr 13 '13 at 14:44
  • Omg sorry... Check the revised version it's not another String on the s... my bad, had a glass of wine... – Johan S Apr 13 '13 at 15:05
  • I know, I removed that myself however I'm still getting the same errors. – Boggy B Apr 13 '13 at 15:07