java - Not getting response of a webservice in android code -
i working on application in have developed ios code making webservice call , giving me response successfully,the same webservice call when trying call in android not working not giving me response,i posting both code,can me figure it?
ios code
nsurl *url = [nsurl urlwithstring:[nsstring stringwithformat:@"%@/%@",k_server_base_address,service_name]]; nsurlsessionconfiguration *config = [nsurlsessionconfiguration defaultsessionconfiguration]; nsurlsession *session = [nsurlsession sessionwithconfiguration:config]; // 2 nsmutableurlrequest *request = [[nsmutableurlrequest alloc] initwithurl:url]; request.httpmethod = @"post"; [request setvalue:[nsstring stringwithformat:@"%@",k_content_type] forhttpheaderfield:@"content-type"]; // 3 nsmutabledictionary *dictionary = [[nsmutabledictionary alloc] init]; [dictionary setobject:[nsstring stringwithformat:@"%@",k_user_name] forkey:@"apiusername"]; [dictionary setobject:[nsstring stringwithformat:@"%@",k_password] forkey:@"password"]; nslog(@"%@",dictionary); nserror *error = nil; nsdata *data_prm = [nsjsonserialization datawithjsonobject:dictionary options:kniloptions error:&error]; nslog(@"%@",[[nsstring alloc] initwithdata:data_prm encoding:nsutf8stringencoding]); if (!error) { // 4 nsurlsessionuploadtask *uploadtask = [session uploadtaskwithrequest:request fromdata:data_prm completionhandler:^(nsdata *data,nsurlresponse *response,nserror *error) { // handle response here.. nslog(@"this data : %@",data);
android code
public class getcontestants extends asynctask<void, void, void> { stringentity se; @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(startactivity.this); pdialog.setmessage("loading..."); pdialog.setcancelable(false); pdialog.show(); // statelist.clear(); } @override protected void doinbackground(void... params) { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://www.fansplay.com/wsfptb20/fbs.svc/getcontestants"); string json = ""; try { // 3. build jsonobject jsonobject jsonobject = new jsonobject(); jsonobject.accumulate("apiusername", "afbk8@dl4ejmd6"); jsonobject.accumulate("country","8gb4he1c-efsd-4l17-vy2d-a27oc8c52f6m"); // 4. convert jsonobject json string json = jsonobject.tostring(); // ** alternative way convert person object json string usin jackson lib // objectmapper mapper = new objectmapper(); // json = mapper.writevalueasstring(person); // 5. set json stringentity se = new stringentity(json); } catch (unsupportedencodingexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } // 6. set httppost entity httppost.setentity(se); // 7. set headers inform server type of content httppost.setheader("accept", "application/json"); httppost.setheader("content-type", "application/json"); try { httpresponse httpresponse = httpclient.execute(httppost); // 9. receive response inputstream inputstream = httpresponse.getentity().getcontent();
in above codes "ios code" working fine , "android code" not working. can please me?
try snippet of code may helpful
public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; string url="http://www.fansplay.com/wsfptb20/fbs.svc/getcontestants"; public jsonparser(){ } public jsonobject makehttprequest(string url){ jsonobject jo = null; jo = new jsonobject(); try { jo.accumulate("apiusername", "afbk8@dl4ejmd6"); jo.accumulate("country","8gb4he1c-efsd-4l17-vy2d-a27oc8c52f6m"); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } jsonobject job=new jsonobject(); try { job.accumulate("aloha", jo); } catch (jsonexception e2) { // todo auto-generated catch block e2.printstacktrace(); } log.e("url", job.tostring()); defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(this.url); try { httppost.setentity(new stringentity(job.tostring(), "utf- 8")); // httppost.tostring(); // log.e("url", httppost.tostring()); } catch (unsupportedencodingexception e1) { // todo auto-generated catch block e1.printstacktrace(); } try { httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } try { bufferedreader reader = new bufferedreader(new inputstreamreader(is,"iso-8859-1"),8); stringbuilder sb = new stringbuilder(); string line = null; try { while((line = reader.readline())!=null){ sb.append(line+"\n"); } is.close(); json = sb.tostring(); log.e("url", json); try { jobj = new jsonobject(json); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } catch (unsupportedencodingexception e) { // todo auto-generated catch block e.printstacktrace(); } return jobj; }
Comments
Post a Comment