Forum Xamarin.Android

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocatio

starlinstarlin DOMember, University ✭✭

Hi i´m having some issue while trying to bind some data from a web api (i´m using WebClient, don´t know if this is the best approach) that respond in json to a ListView, here is the relevant code:


Button button = FindViewById (Resource.Id.myButton);

      button.Click += delegate {
          var lista = FindViewById<ListView>(Resource.Id.listView1);

          WebClient client = new WebClient();
          client.DownloadStringAsync( new Uri(""));
          client.DownloadStringCompleted += (object sender, DownloadStringCompletedEventArgs e) => {
              var data = JsonConvert.DeserializeObject<IEnumerable<PeliculaModel>>( e.Result);
              lista.Adapter = new PeliculaAdapter( this, data);



public class PeliculaAdapter : BaseAdapter
Activity context;
IEnumerable items;
public PeliculaAdapter( Activity context, IEnumerable items){
this.context = context;
this.items = items;

  #region implemented abstract members of BaseAdapter

  public override Java.Lang.Object GetItem (int position)
      return null;

  public override long GetItemId (int position)
      return items.ElementAt (position).idPelicula;

  public override View GetView (int position, View convertView, ViewGroup parent)
      View view = convertView;
      if (view == null)
          view = context.LayoutInflater.Inflate (Resource.Layout.ListViewPelicula, null);
      view.FindViewById<TextView> (Resource.Id.textView1).Text = items.ElementAt(position).titulo;
      return view;


  public override int Count {
      get {
          return items.Count();



And finaly PeliculaModel.cs

public class PeliculaModel
public int idPelicula;
public String titulo, img;

Have to mention, that if i don´t use the web service and instead of that i insert records to the to the Ienumerable everything work just fine.

Thanks before all


  • starlinstarlin DOMember, University ✭✭

    Anyone got something? what approach are you using for web api and android?

  • YSharpYSharp USMember ✭✭
    edited January 2014

    Refresher about this exception in .NET:


    So, as its name implies (or, well, tries to, anyway) it is thrown by System.Reflection when the latter catches an exception, while attempting to query / manipulate / invoke one of the member of the instance reflected upon. That exception's InnerException may or may not provide more info (YMMV), but in any case, anyway, it means the actual culprit is always the object reflected upon, precisely.

    More concretely to your case, you're in the context of some native GUI calling back into your managed .NET code to trigger the OnClick event on your button - and that, by definition, necessarily implies that the native GUI, thru one or more layers in between, will have to, soon or late, to use .NET's System.Reflection in order to find out which managed object instance has the event + handler which corresponds to the user's physical click action.

    So, from this sentence:

    "Have to mention, that if i don´t use the web service and instead of that i insert records to the to the Ienumerable everything work just fine"

    ... and proceeding by elimination, we can deduce that the issue (throwing the original exception that got "mapped" into a System.Reflection.TargetInvocationException) is very likely involving either one of these two guys:

          client.DownloadStringAsync( new Uri(""));


          client.DownloadStringCompleted += (object sender, DownloadStringCompletedEventArgs ...)

    I would assume that you probably verified already the service URL and that your accessing it in the former doesn't just yell a rude 404 back at you... which leaves us with the latter, for another good candidate culprit.

    My suggestion: make as many unit tests as necessary to verify that you understand and know how to use JSON.NET (as it seems that's what you're using) for the various JSON payloads that you get and try to deserialize in your enumerable of "PeliculaModel".

    Both JSON is a friendly textual data interchange format, as well as JSON.NET a friendly library with a flexible interface (w/ its Serialize/Deserialize methods + overloads), but there is still some minimal discipline to be aware of and to follow (refer to the documentation) when using the latter to translate JSON either from / to .NET strings / objects.


  • nefgrixisnefgrixis Member ✭✭

    @YSharp can you explain that a bit simpler please?

Sign In or Register to comment.