0

im want to send a http post with a json to my webservice. i know the web service is working because im using Postman to test it.

My web service code is :

namespace ProyectoFinal.Controllers
 {
[Authorize]
public class HomeController : Controller
{
    private PatientModelDBContext db = new PatientModelDBContext();
    private RootObjectRepository repo = new RootObjectRepository();

    public ActionResult Index()
    {
        ViewBag.Message = "Sistema Medico Integrado";

        return View();
    }

    public ActionResult About()
    {
        ViewBag.Message = "Your app description page.";

        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
    //[HttpPost]
    [AllowAnonymous]
    public ActionResult Insert(string data)
    {
        try
        {
            var recibido = JsonConvert.DeserializeObject<RootObject>(data);
            repo.InsertOrUpdate(recibido);
            repo.Save();
        }
        catch (Exception err)
        {
            return new JsonResult { Data = err.ToString() };
        }
        return new JsonResult { Data = "Eureka" };
    }

    public ActionResult EmergencyRoom()
    {
        return View(db.RootObjects.ToList());
    }

}

}

The code in android is :

HttpClient client = new DefaultHttpClient();
    try {
        String SendBookingURL= "http://pruebaproyectosmi.azurewebsites.net/home/Insert?data=";
        HttpPost post = new HttpPost(SendBookingURL);       
        HttpResponse response;
        String klk =  URLEncoder.encode(json, "UTF-8");

        StringEntity se = new StringEntity(klk); 
        System.out.println("URL: " + klk);
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "POST/"));
        post.setEntity(se);

        try {
            response = client.execute(post);
            System.out.println("URL: " + response);
            HttpEntity entity = response.getEntity();
            if(entity != null) {
                String ResponseSummaryTable = EntityUtils.toString(entity);
                System.out.println("body" + ResponseSummaryTable);
            }
        }
          catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), (CharSequence) e, 3000).show();
            }
       }
            catch(Exception e){
                e.printStackTrace();
            }      

and the error that is giving me is :

I/System.out(17968): body"System.NullReferenceException: Object reference not set to an instance of an object.\r\n   at ProyectoFinal.Models.RootObjectRepository.InsertOrUpdate(RootObject rootobject)\r\n   at ProyectoFinal.Controllers.HomeController.Insert(String data)"

When the web service get the json it suppost to return "eureka";

guelo
  • 301
  • 3
  • 10
  • I found the solution here : [1]: http://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data/4073451#4073451 – guelo Nov 26 '13 at 04:36

1 Answers1

3

Here is the thing you need to know

http://android.programmerguru.com/android-json-web-service-tutorial/ and try this also

http://android.programmerguru.com/how-to-call-asp-net-web-service-in-android/

Hope this will help you :)

khurram
  • 1,334
  • 11
  • 24