utf 8 - Send utf-8 encoded strings in Android HTTP request -


i have made http-post inside android application. values sent strings app webserver. problem is, values not in utf-8 want them be. webserver has utf-8 encoding know there code inside app need change. see snippet below:

private void sendpostrequest(string facebookid, string name, string email) {          class sendpostreqasynctask extends asynctask<string, void, string>{              @override             protected string doinbackground(string... bcs) {                  string bcfacebookid = bcs[0];                 string bcname = bcs[1];                 string bcemail = bcs[2];                    httpclient httpclient = new defaulthttpclient();                  httppost httppost = new httppost("url");                  basicnamevaluepair facebookidbasicnamevaluepair = new basicnamevaluepair("bcfacebookid", bcfacebookid);                 basicnamevaluepair namebasicnamevaluepair = new basicnamevaluepair("bcname", bcname);                 basicnamevaluepair emailbasicnamevaliepair = new basicnamevaluepair("bcemail", bcemail);                  list<namevaluepair> namevaluepairlist = new arraylist<namevaluepair>();                 namevaluepairlist.add(facebookidbasicnamevaluepair);                 namevaluepairlist.add(namebasicnamevaluepair);                 namevaluepairlist.add(emailbasicnamevaliepair);                 try {                      urlencodedformentity urlencodedformentity = new urlencodedformentity(namevaluepairlist);                      httppost.setentity(urlencodedformentity);                      try {                         httpresponse httpresponse = httpclient.execute(httppost);                          inputstream inputstream = httpresponse.getentity().getcontent();                          inputstreamreader inputstreamreader = new inputstreamreader(inputstream);                          bufferedreader bufferedreader = new bufferedreader(inputstreamreader);                          stringbuilder stringbuilder = new stringbuilder();                          string bufferedstrchunk = null;                          while((bufferedstrchunk = bufferedreader.readline()) != null){                             stringbuilder.append(bufferedstrchunk);                         }                          return stringbuilder.tostring();                      } catch (clientprotocolexception cpe) {                          cpe.printstacktrace();                     } catch (ioexception ioe) {                         system.out.println("second exception caz of httpresponse :" + ioe);                         ioe.printstacktrace();                     }                  } catch (unsupportedencodingexception uee) {                     system.out.println("an exception given because of urlencodedformentity argument :" + uee);                     uee.printstacktrace();                 }                  return null;             } 

for example, letter 'ö' becomes '?'. how fix this? cheers!

the biggest single reason characters converted question marks conversion of characters bytes, , characters, not matching.

the code have supplied has line:

inputstreamreader inputstreamreader = new inputstreamreader(inputstream); 

this problematic because not specifying how convert bytes characters. instead want this:

inputstreamreader inputstreamreader = new inputstreamreader(inputstream, "utf-8"); 

what specify character encoding depend upon character encoding have specified elsewhere. without specifying character encoding, "default" character encoding, , depends upon settings in both client , server. java uses unicode, , utf-8 encoding preserve characters java allows.

for debugging, may want use inputstream , retrieve bytes that, , print out byte values, in order verify indeed utf-8 encoded representations of original character values. proper encoding of 'ö' (x00f6) 'ö' (x00c3 x00b6).

you need assure original post request utf-8 encoded. urlencodedformentity class uses default character encoding, might not utf-8. change this:

urlencodedformentity uefe = new urlencodedformentity(namevaluepairlist); 

to

urlencodedformentity uefe = new urlencodedformentity(namevaluepairlist, "utf-8"); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -