Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extra character after writeUTF

Tags:

java

android

I have an Android app. It send data to Java-based programm. Android code:

      Socket socket = new Socket(InetAddress.getByName("10.0.3.2"), 5555);

        OutputStream sout = socket.getOutputStream();
        DataOutputStream out = new DataOutputStream(sout);


        out.writeUTF(jsonLoginData.toString()); 

//       IN jsonLoginData i put some values in another part of code
//        jsonLoginData = new JSONObject();
//        jsonLoginData.put("login",login);
//        jsonLoginData.put("password",password);

        out.flush();
        out.close();

So in another side i have Java based proram which is listenning 5555 port and getting data:

// var "s" is a socked listening 5555 port 

InputStream is = s.getInputStream();

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(s.getInputStream()));

  String line;
  String r = "";
  StringBuilder content = new StringBuilder(); 

      // read from the urlconnection via the bufferedreader
            while ((line = bufferedReader.readLine()) != null)
           {
             r = r + line;
           }

   bufferedReader.close();

    System.out.println("\n ==>" + r);

But the problem is that every time i have extra character in var "r". Console shows me:

==> <{"password":"6f3fa6ad89a459af8e1819c514059b3","login":"456"} 

==> ={"password":"e516fac382793b462d6a19fa849db53a","login":"456"} 

==> B{"password":"6f3fa6ad89a459af8e1819c514059b3","login":"456111414"} 

As you can see there is strange symboles before "{...}"like "<" or "=" or "B". Don`t understand why id going so...

like image 709
dantey89 Avatar asked Jun 28 '26 07:06

dantey89


1 Answers

Don't use DataOutputStream. Just write your bytes to the OutputStream.

OutputStream sout = socket.getOutputStream();
byte[] bytes = jsonLoginData.toString().getBytes("UTF-8");
sout.write(bytes);
like image 177
ZhongYu Avatar answered Jun 30 '26 21:06

ZhongYu