Good morning, I am developing an Android application that sends a Json to a Web API by POST, but returns an ExceptionType, System.NullReferenceException.
The Android code is as follows:
JSONObject jobject = new JSONObject();
jobject.put("Valor1",1);
jobject.put("Valor2","hola");
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
post.addHeader("Authorization", "Bearer " + token);
StringEntity entity = new StringEntity(jobject.toString());
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(entity);
HttpResponse response = client.execute(post);
String respStr = EntityUtils.toString(response.getEntity());
Log.d(TAG, "RESPUESTA DEL WEB API: " + respStr);
The Web API code is as follows:
public class Datos {
public int Valor1 { get; set; }
public string Valor2 { get; set; }
}
[Authorize]
public class ValoresController : ApiController {
public string Post([FromBody]Datos datos) {
return "Valor 2: " + datos.Valor2; // Linea 81
}
}
The answer is this:
RESPUESTA DEL WEB API: {
"Message":"Error.",
"ExceptionMessage":"Referencia a objeto no establecida como instancia de un objeto.",
"ExceptionType":"System.NullReferenceException",
"StackTrace":" en Ejemplo.Controllers.ValoresController.Post(Datos datos) en c:\Users\Usuario\Documents\Visual Studio 2013\Projects\Ejemplo\Ejemplo\Controllers\ValoresController.cs:línea 81\r\n en lambda_method(Closure , Object , Object[] )\r\n en System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n en System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary'2 arguments, CancellationToken cancellationToken)\r\n--- Fin del seguimiento de la pila de la ubicación anterior donde se produjo la excepción ---\r\n en System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n en System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n en System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- Fin del seguimiento de la pila de la ubicación anterior donde se produjo la excepción ---\r\n en System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n en System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n en System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- Fin del seguimiento de la pila de la ubicación anterior donde se produjo la excepción ---\r\n en System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n en System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n en System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()\r\n--- Fin del seguimiento de la pila de la ubicación anterior donde se produjo la excepción ---\r\n en System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n en System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n en System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.MoveNext()\r\n--- Fin del seguimiento de la pila de la ubicación anterior donde se produjo la excepción ---\r\n en System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n en System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n en System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
}
Let's see if someone who has written something like my example can give me a tip, thanks in advance.