Android Incorrect value when converting String to StringEntity -


i sending string server using httppost. string jsonobject thath have converted string far working properly... must sent content-type "application / json".

to send it, convert string stringentity , add httppost.setentity (my_string) ... problem server tells me not recognize data (is ready receive jsonobject converted string)

in code, use log know value of string before converting stringentity, , result:

string: {"gender":"male","user":"pedro","email":"ejemplo@ejemplo.com","language":"english"} 

however, when convert string stringentity, result of log, instead of being same, follows:

string: org.apache.http.entity.stringentity@16a4bc74 

why?

i not know i'm doing wrong ...

i searched , found many examples, think i'm doing correctly ... not understand error. why string, when converted stringentity not maintained values?

i have tried many examples, such http://hmkcode.com/android-send-json-data-to-server/, , many more.

this code.

i appreciate much.

greetings.

public static jsonobject makeservicecall(string url, jsonobject params) {

try {      log.i("dmode", "llegan los valores:" + " " + params);      defaulthttpclient httpclient = new defaulthttpclient();     httpentity httpentity = null;     httpresponse httpresponse = null;     httppost httppost = new httppost(url);       if (params != null) {          string conversion = params.tostring();          log.i("dmode", "jsonboject string" + " " + conversion);          stringentity se;         se = new stringentity(conversion);          log.e("dmode", "string stringentity" + " " + se);          // set http parameters         httppost.setheader("accept", "application/json");         httppost.setheader("content-type", "application/json");         httppost.setentity(se);      }      string response = null;      httpresponse = httpclient.execute(httppost);     httpentity = httpresponse.getentity();     response = entityutils.tostring(httpentity);      try {         jobject = new jsonobject(response);     } catch (exception e) {      }      log.i("dmode", "devoluciĆ³n" + jobject);  } catch (unsupportedencodingexception e) {     e.printstacktrace(); } catch (clientprotocolexception e) {     e.printstacktrace(); } catch (ioexception e) {     e.printstacktrace(); }  return jobject; 

}

try using url connection :

public jsonobject readjsonfromurl(string urlstring, jsonobject param){         string response = null;         jsonobject jobject = null;         try {                        url url = new url(urlstring);             urlconnection conn = url.openconnection();             conn.setdooutput(true);             list<namevaluepair> params = new arraylist<namevaluepair>();             params.add(new basicnamevaluepair("dmode", param.tostring()));             outputstream os = conn.getoutputstream();             bufferedwriter writer = new bufferedwriter(new outputstreamwriter(os, "utf-8"));             writer.write(getquery(params));             writer.flush();             writer.close();             os.close();             conn.connect();             bufferedinputstream in = new bufferedinputstream(conn.getinputstream());             response = getstringfrominputstream(in);             jobject = new jsonobject(response);         } catch (unsupportedencodingexception e) {             log.e("error", "unsupportedencodingexception " + e.tostring());         }  catch (ioexception e) {             log.e("error", "ioexception " + e.tostring());         } catch (jsonexception e) {             log.e("error", "jsonexception: " + e.tostring());         }         return jobject;     }      private string getquery(list<namevaluepair> params) throws unsupportedencodingexception     {         stringbuilder result = new stringbuilder();         boolean first = true;         (namevaluepair pair : params)         {             if (first)                 first = false;             else                 result.append("&");             result.append(urlencoder.encode(pair.getname(), "utf-8"));             result.append("=");             result.append(urlencoder.encode(pair.getvalue(), "utf-8"));         }         return result.tostring();     }      private string getstringfrominputstream(inputstream is) {          bufferedreader br = null;         stringbuilder sb = new stringbuilder();         string line;         try {             br = new bufferedreader(new inputstreamreader(is));             while ((line = br.readline()) != null) {                 sb.append(line);             }         } catch (ioexception e) {             e.printstacktrace();         } {             if (br != null) {                 try {                     br.close();                 } catch (ioexception e) {                     e.printstacktrace();                 }             }         }         return sb.tostring();     } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -