0
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.

Alireza Madad
  • 151
  • 2
  • 13
  • Is your server expecting a JSON value to process?? Your client code is good and it should work(i havent tested it, but i m not able to point out any mistake as such), try hitting it with POSTMAN, use the same JSON as request. you will get to know where you are wrong. – Ashwani Apr 23 '18 at 10:51
  • via post man post method and same json it has no problem but i detected that input object of web api method is null even though it is fired – Alireza Madad Apr 23 '18 at 11:37
  • add this to your call and check setRequestProperty("Content-Type", "application/json; charset=UTF-8"); – Ashwani Apr 23 '18 at 11:44
  • i added your code but still input object of api is null – Alireza Madad Apr 23 '18 at 11:49
  • place breakpoints over here ( String json = gson.toJson(o);) and over here : writer.close(); check are you getting any values from here?? – Ashwani Apr 23 '18 at 11:51
  • In my opinion Gson is not able to convert it to string or some mismaching is there... – Ashwani Apr 23 '18 at 11:51
  • maybe out put stream does not write json if that what should i do – Alireza Madad Apr 23 '18 at 11:52
  • check the second answer given over here...it will write json.. https://stackoverflow.com/questions/21404252/post-request-send-json-data-java-httpurlconnection – Ashwani Apr 23 '18 at 11:54
  • json is created as "{"GENDER:1,"ID":0,"NAME":"NA","USAGE":0}" – Alireza Madad Apr 23 '18 at 11:58
  • did you checked other answer? – Ashwani Apr 23 '18 at 12:23
  • yes my code is exactly like other answer and i get specific error from api object not set to an instance of an object(system null reference exception) – Alireza Madad Apr 23 '18 at 13:20

0 Answers0