Do POST request with Android DefaultHTTPClient cause freeze on execute() -


i need post data server. use code:

httpclient client = new defaulthttpclient(); try {      httppost httppost = new httppost(serverurl);      stringentity se = new stringentity(data);     httppost.setentity(se);     httppost.setheader("accept", "application/json");     httppost.setheader("content-type", "application/json");      // execute http post request     httpresponse response = client.execute(httppost);     int statuscode = response.getstatusline().getstatuscode();     log.i(tvprogram.tag, "errorhandler post status code: " + statuscode);  } catch (exception e) {     e.printstacktrace(); } {     if (client != null) {         client.getconnectionmanager().shutdown();     } } 

but problem android freeze on execute() method, application blocked out , after time android tell me application doesn't respond.

i tried debug sdk classes , freeze in abstractsessioninputbuffer class on line 103

l = this.instream.read(this.buffer, off, len); 

i tried run request in separated thread, same problem.

i tested on android 2.1 (emulator) , android 2.2 real mobile device.
tried set http proxy , use fiddler check http communication data received server , server send correct answer , http code 200. seems ok.

what wrong please?

update: when use androidhttpclient part of android 2.2 sdk works great. not in earlier version of android. include it's source code in app now. androidhttpclient use defaulthttpclient internally, problem in configuration of defaulthttpclient.

i using post http request successfully. here code. removed pieces using handler display messages etc. , handler itself. post string "&name=value@name2=value2"...

protected class connectingthread implements runnable {               message msg;     private handler mexthandler;     private string mdata;     private string murl;      /**       * @param h (handler) - handler messages thread      * @param data (string) - data send in http request's post      * @param url (string) - url connect      */     connectingthread(handler h, string data, string url) {         mexthandler = h;         mdata = data;         murl = url;     }      public void run() {                  try {             // todo use handler display txt info connection              url url = new url(murl);             urlconnection conn = url.openconnection();                          conn.setconnecttimeout(conn_timeout_millis);              conn.setdooutput(true);             bufferedoutputstream wr = new bufferedoutputstream(conn.getoutputstream());                             wr.write(mdata.getbytes());             wr.flush();             wr.close();              string sreturn = null;              bufferedreader rd = new bufferedreader(new inputstreamreader(conn.getinputstream()));             int length = conn.getcontentlength();             char[] buffer = new char[length];             int read = rd.read(buffer);             if(read == length)                 sreturn = new string(buffer);             rd.close();             buffer = null;             // todo use handler use response          } catch (exception e) {             //....         }           // todo use handler display txt info connection error        }  }            

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -