-2

It's my first project. When i use Post,I can't send the data to server.

Here is my code:

JSONObject json = new JSONObject();
        try
        {          json.put("_username", mUsername);
                   json.put("_password", mPassword);
                   String s=json.toString();
                   System.out.println("string: " + s);
                   StringEntity se = new StringEntity( s,HTTP.UTF_8);
                   System.out.println("0.string: " + se);
                   se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded"));
                   System.out.println("1.string: " + se);
                   httpRequest.setEntity(se);

And it catch (Exception e) Here is logcat:

07-05 13:09:57.731 16809-16809/com.ad_imagine.jsonconnection I/System.out: string: {"_username":"lorem@ipsum.fr","_password":"aze"}
07-05 13:09:57.733 16809-16809/com.ad_imagine.jsonconnection I/System.out: 0.string: org.apache.http.entity.StringEntity@3117948
07-05 13:09:57.733 16809-16809/com.ad_imagine.jsonconnection I/System.out: 1.string: org.apache.http.entity.StringEntity@3117948
07-05 13:09:57.768 16809-16809/com.ad_imagine.jsonconnection I/System.out: 6.Exception: null

What is org.apache.http.entity.StringEntity@3117948? Why 0.string is not correct?

Ling QI
  • 17
  • 6

1 Answers1

0

What is org.apache.http.entity.StringEntity@3117948?

org.apache.http.entity.StringEntity@3117948 is the result of se.toString(). StringEntity does not seem to override toString() so the implementation of Object.toString() is used which just returns <FullyQualifiedClassName>@<hashCode>

Why 0.string is not correct?

You can't tell its not correct. You have to read from se.getContent()

larsgrefer
  • 2,387
  • 15
  • 29
  • You don't have to add `se.toString()`, the compiler added that for you. `System.out.println("0.string: " + se);` and `System.out.println("0.string: " + se.toString());` are exaclty the same. You have to call se.getContent() and read from it. – larsgrefer Jul 05 '16 at 14:11
  • but how to change the code? I want `se` to be `_username=lorem@ipsum.fr&_password=aze`,then i can send it to server – Ling QI Jul 05 '16 at 14:12
  • The content of `se` is `{"_username":"lorem@ipsum.fr","_password":"aze"}` – larsgrefer Jul 05 '16 at 14:14
  • i have added getContent(), it changes to ` 0.string: java.io.ByteArrayInputStream@714c3e1` – Ling QI Jul 05 '16 at 14:17
  • The class `StringEntity` wraps a String (in your case some JSON data in `s`) in an `InputStream` (that object returned by `se.getContent()`). If you wan't to get the String back from the `InputStream` look here: http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string – larsgrefer Jul 05 '16 at 14:25