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...
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With