I am new to android development. I have an issue with consuming complex types from web api.
Scenario::
I have created a web api method using visual studio for checking user credentials from my sql 2012 database.
API method::
[HttpPost]
public Login PostLogin(JObject jsonData)
{
dynamic json = jsonData;
string Jusername = json.username.Value;
string JPassword = json.Password.Value;
int JIsEncrypted = Convert.ToInt32(json.IsEncrypted.Value);
Login m = db.sp_GetValidUserLoginDetails(Jusername, JPassword,JIsEncrypted)
.Select(x => new Login()
{
Employee_Id = x.Employee_Id,
Employee_Code = x.Employee_Code,
Employee_FirstName = x.Employee_FirstName,
Employee_LastName = x.Employee_LastName,
Employee_EmailId = x.Employee_EmailId,
Employee_ContactNumber = x.Employee_ContactNumber,
IsActive = Convert.ToBoolean(x.IsActive),
Created_Date = x.Created_Date,
Modified_Date = x.Modified_Date,
User_Name = x.User_Name,
User_Password = x.User_Password,
error = x.error
}).ToList().FirstOrDefault();
return m;
}
}
In Android project I have tried below two methods
Method 1::
public String requestWebService(String serviceUrl, login logg) throws UnsupportedEncodingException {
InputStream inputStream = null;
JSONObject log = new JSONObject();
String result = "";
try {
log.put("username", logg.username);
log.put("Password", logg.password);
log.put("IsEncrypted", 0);
} catch (JSONException e) {
e.printStackTrace();
}
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
String json = "";
json = log.toString();
httpPost.setEntity(new StringEntity(log.toString(), "UTF-8"));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
try {
HttpResponse httpResponse = httpclient.execute(httpPost);
if(inputStream != null)
result = convertInputStreamToString(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(this, "done", Toast.LENGTH_LONG).show();
return result;
}
Method 2::
public String Requesting(String url, login log) throws IOException, JSONException {
URL object=new URL(url);
HttpURLConnection con = (HttpURLConnection) object.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestMethod("POST");
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(log.toString());
wr.flush();
StringBuilder sb = new StringBuilder();
int HttpResult = con.getResponseCode();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
System.out.println(""+sb.toString());
return null;
}
Question::
How can I pass my username and password as complex type collection from android app to this webapi and get the response back in android project? I am getting null JsonObject everytime.
Please help.Thanks in advance
I have checked below link but here but this isnt working in my case. Android App with ASP.NET WebAPi Server - send complex types