java - http post request for android -
i want send post request in java android.
i use code :
httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("myurl"); try { // add data list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("artist", "amy macdonald")); namevaluepairs.add(new basicnamevaluepair("title", "don't tell me it's over")); namevaluepairs.add(new basicnamevaluepair("album", "don't tell me it's over")); httppost.setentity(new urlencodedformentity(namevaluepairs)); // execute http post request httpresponse response = httpclient.execute(httppost); } catch (clientprotocolexception e) { // todo auto-generated catch block } catch (ioexception e) { // todo auto-generated catch block }
the problem when use :
httpresponse response = httpclient.execute(httppost);
i exception, , webservice working sure,
what scheme posting to? http or https? have set client correctly clientconnectionmanager
, httpparams
? exceptions getting in logs?
the difference see between code have posting data (assuming client setup correctly) use httpcontext in execute method this:
httppost = new httppost(urlstring); httppost.setentity(new urlencodedformentity(nvps)); response = httpclient.execute(httppost, httpcontext); statuscode = response.getstatusline().getstatuscode();
where context setup in constructor with
httpcontext = new basichttpcontext(); httpcontext.setattribute(clientcontext.cookie_store, new basiccookiestore());
if using https, you'll need setup client more information can handle keystore requirements , other secure aspects of connection.
Comments
Post a Comment