public long SetHuman(HUMAN human)//string Name,byte Gender,short Usage)
{
using (KARYABDBEntities db = new KARYABDBEntities())
{
try
{
//HUMAN human = new HUMAN();
//human.NAME = Name;
//human.GENDER = Gender;
//human.USAGE = Usage;
db.Configuration.ProxyCreationEnabled = false;
db.HUMen.Add(human);
db.SaveChanges();
return human.ID;
}
catch(Exception e)
{
return 0;
}
}
}
This is my call to API
public class HttpMadad {
public static Long postObjectAndGetID(Object o,String Api) {
String result=null;
try {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
URL url = new URL(Api);
Gson gson = new Gson();
String json = gson.toJson(o);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("POST");
urlConnection.connect();
OutputStream outputStream = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(json.toString());
writer.close();
outputStream.close();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
result = sb.toString();
bufferedReader.close();
urlConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
return Long.parseLong(result);
}
I always get null result or zero result from API my guess is input object of webapi is null or something else. It is supposed to send JSON to API and get a JSON as a long and be parsed to long.I don't know where is the problem exactly. I used query string and it worked.I don't know why it is not working with JSON
Thank you in advance.