how to use URL to post data with HttpClient in android -
i have send/post data .svc
web service connect remote database. i'm using jsonstringer
send data every time response status false. data not sent. how use httppost
in android . can me how solve .
here webservice code
string namespace = "http://103.24.4.60/xxxxx/mobileservice.svc"; public void activityupload( final string strcurrentdatetime, final string strtitle, final string replacedescchar, final string editedhashtag) { new asynctask<string, void, string>() { @override protected string doinbackground(string... arg0) { string line = ""; try { log.e("actiondate "," = "+ strcurrentdatetime); log.e("activityid"," = "+stractivityid); log.e("userid"," = "+str_userid); log.e("objectid"," = "+strvessid); log.e("name"," = "+strtitle); log.e("remark"," = "+replacedescchar); log.e("status"," = "+"pending"); log.e("type"," = "+strtype); log.e("starflag"," = "+0); log.e("hashtag"," = "+editedhashtag); log.e("authentication_token"," = "+str_authentication_token); // make web service connection httppost request = new httppost(namespace + "/upd_post_activity"); request.setheader("accept", "application/json"); request.setheader("content-type", "application/json"); // build json string jsonstringer testapp = new jsonstringer().object() .key("actiondate").value(strcurrentdatetime) .key("activityid").value(stractivityid) .key("userid").value(str_userid) .key("objectid").value(strvessid) .key("name").value(strtitle) .key("remark").value(replacedescchar) .key("status").value("pending") .key("type").value(strtype) .key("starflag").value("0") .key("hashtag").value(editedhashtag) .key("authentication_token").value(str_authentication_token).endobject(); stringentity entity = new stringentity(testapp.tostring()); log.d("****parameter input****", "testing:" + testapp); request.setentity(entity); // send request wcf service defaulthttpclient httpclient = new defaulthttpclient(); httpresponse response = httpclient.execute(request); log.d("webinvoke", "saving: " + response.getstatusline().tostring()); // status of web service bufferedreader rd = new bufferedreader(new inputstreamreader( response.getentity().getcontent())); // print status in log while ((line = rd.readline()) != null) { log.d("****status line***", "webservice: " + line); } } catch (exception e) { e.printstacktrace(); } return line; } }.execute(); }
here input parameter.
****parameter input****﹕ testing:{"actiondate":"2016-01-21%2014:20:43%20pm","activityid":"120160119180421058","userid":"125","objectid":"1","name":"title2","remark":"test%20two","status":"pending","type":"3","starflag":"0","hashtag":"990075","authentication_token":"6321d079-5b28-4f3f-aee7-d59a1b9efa59"}
thanks in advanced.
realize android httpclients in process of deprecation ( in favor of httpsurlconnection ) but, these httpclients still used pretty widely. on gradle builds, regard deprication, , small dependency lib tweeks , httpclient may used time still.
( still gonna use httpclient ? )
put android aside min.
- learn how curl json body tests show exact json in body , exact headers need success http result post ... ref here
once have can go transferring curl test's components on android.httpclient.exec.post using httpclient of choice.
- set same group of headers had on in curl tests in android post. apache.httpclient sample
2.a. make sure default list of headers clients 'request' constructor not include default headers not want... in order assure of ,you need turn on header logging client.... java example logger . remove unnecessary headers included framework constructor of post.
2.b android logger (wire, headers) diff , may take digging , depend on client in use.
- with same headers curl tests, set http.posts request.entity either string or encoded array of bytes containing same json body used in curl tests.
3.a. depending on json lib, create message objects , convert objects friendly type enclosure in entity post ie use 'writer' convert objects serialized string json.
reqrole = new objectmapper().createobjectnode(); reqrole.put("__type", "pointer"); reqrole.put("classname", "_role"); reqrole.put("objectid", roleid); rootob.put("requestedrole", reqrole); rootob.put("requestedby",usersarr); stringwriter writer = new stringwriter(); try { new objectmapper().writevalue(writer, rootob) .. string http-post-str=writer.tostring();
3.b. wrap string json in post request...
httppost.setentity(new stringentityhc4(http-post-str));
- exec request , youll same results got in curl because headers same or same , body same , encoded string of json. same input = same result
Comments
Post a Comment