c# - Android post request to wcf service -


i'm posting data android wcf service getting bad request error using httpurlconnection.

android code:

`public user registerpost(context context, user ruser) throws unsupportedencodingexception{      string url = context.getresources().getstring(r.string.service_url)+ "signup";     try     {         jsonobject uservalue =  new jsonobject();         jsonobject user =  new jsonobject();          uservalue.put("name", ruser.name);         uservalue.put("email", ruser.email);         uservalue.put("password", ruser.password);         uservalue.put("phone",  ruser.phone);         uservalue.put("nic", ruser.nic);         uservalue.put("usertype", ruser.usertype);         uservalue.put("image", ruser.image);          user.put("user", uservalue.tostring());          string response = postdata(url, user);      }     catch(exception ex)     {         ex.printstacktrace();     }     return  null;    }` 

postdata() function in same file.

public string postdata(string urlpath, jsonobject json)   {     httpurlconnection connection = null;     try {         url url=new url(urlpath);         connection = (httpurlconnection) url.openconnection();         connection.setdooutput(true);         connection.setdoinput(true);         connection.setrequestmethod("post");         connection.setrequestproperty("content-type", "application/json");         connection.setrequestproperty("accept", "application/json");         outputstreamwriter streamwriter = new outputstreamwriter(connection.getoutputstream());         log.d("write data",json.tostring());         streamwriter.write(json.tostring());         streamwriter.flush();         stringbuilder stringbuilder = new stringbuilder();         if (connection.getresponsecode() == httpurlconnection.http_ok){             inputstreamreader streamreader = new inputstreamreader(connection.getinputstream());             bufferedreader bufferedreader = new bufferedreader(streamreader);             string response = null;             while ((response = bufferedreader.readline()) != null) {                 stringbuilder.append(response + "\n");             }             bufferedreader.close();              log.d("http_ok response", stringbuilder.tostring());             return stringbuilder.tostring();         } else {             log.e("else response", connection.getresponsemessage());             return null;         }     } catch (exception exception){         log.e("test", exception.tostring());         return null;     } {         if (connection != null){             connection.disconnect();         }     }  } 

json data sending service;

{ "user": "{\"name\":\"rrr rrr\",\"email\":\"rrr@eemail.com\",\"password\":\"123\",\"phone\":\"12333333\",\"nic\":\"44444444\",\"usertype\":\"passenger\",\"image\":\"55akasdfadphpoijpiojasdfasdfasdfasdfasdfasdfasdfasdf\"}" } 

here wcf service code;

 [operationcontract]     [webinvoke(method = "post", uritemplate = "/signup",                  requestformat = webmessageformat.json,                 responseformat = webmessageformat.json)]     bool signup(returnuserdata user);    public bool signup(returnuserdata user)     {          user usr = new user();         usr.name = user.name;         usr.email = user.email;         usr.password = user.password;         usr.phone = user.phone;         usr.nic = user.nic;         usr.usertype = user.usertype;         usr.image = user.image;         db.users.add(usr);         db.savechanges();         return true;     }    public class returnuserdata  {      public string id { get; set; }     public string name { get; set; }     public string email { get; set; }     public string password { get; set; }     public string phone { get; set; }      public string usertype { get; set; }      public string image { get; set; }  } 

finally figured out issue, have wrong implementation of registerpost() function, change function.

public user registerpost(context context, user ruser) throws unsupportedencodingexception{      string url = context.getresources().getstring(r.string.service_url)+ "signup";     try     {         jsonobject newuser =  new jsonobject();         newuser.put("name", ruser.name);         newuser.put("email", ruser.email);         newuser.put("password", ruser.password);         newuser.put("phone",  ruser.phone);         newuser.put("nic", ruser.nic);         newuser.put("usertype", ruser.usertype);         newuser.put("image", ruser.image);          string response = postdata(url, newuser.tostring());         log.d("response", response);          if(response!="")         {             try {                 jsonobject user = new jsonobject(response);                 user returnuser = new user( user.getint("id"),user.getstring("name"),user.getstring("email"),user.getstring("password"),user.getstring("phone"),user.getstring("nic"),user.getstring("usertype"), user.getstring("street"), user.getstring("city"), user.getstring("country"), double.parsedouble(user.getstring("lat")), double.parsedouble(user.getstring("lng")),user.getint("is_login"),user.getint("is_vehicle_added"), user.getstring("reg_id"), user.getint("iserror"), user.getstring("errormessage"), user.getstring("image"));                 return returnuser;             } catch (jsonexception e) {                 e.printstacktrace();             }         }      }     catch(exception ex)     {         ex.printstacktrace();     }     return  null; } 

Comments