Android Web Server Request

Viewed 48

I'm new in Xamarin, but i have good foundation in c#.
Could someone help me do a request to a web server which i start on a win Console application ?

Web server does its work when i call it using Chrome browser and the function Invio() works if it is used in an other Win Console app.

I'm running the app on Android 6.0.1 API23.

I've tried everything but nothing seems works...

This code make the android app freezes:

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme",MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        Button btn_Invia = FindViewById<Button>(Resource.Id.btn_Invia);
        TextView txtV_Debug = FindViewById<TextView(Resource.Id.txtV_Debug);

        btn_Invia.Click += (o, e) => Invio();

    }

    private void Invio()
    {
        using (var ws = new WebClient())
        {
            try
            {
                ws.DownloadString("http://192.168.56.1:4100/1/2/3/4/5");
            }
            catch (Exception ex) { }
        }
    }
}

and xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:text="INVIA"
        android:layout_width="wrap_content"
        android:layout_height="56.0dp"
        android:minWidth="25px"
        android:minHeight="25px"
        android:id="@+id/btn_Invia"
        android:layout_alignParentRight="true"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="0.0dp"
        android:layout_marginLeft="21.5dp"
        android:layout_marginRight="21.5dp"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20.5dp" />
    <TextView
        android:id="@+id/txtV_Debug"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Posso inciampare ma non cadere..."
        android:layout_alignParentRight="true"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="28.0dp"
        android:layout_marginLeft="21.5dp"
        android:layout_marginRight="21.5dp" />
</RelativeLayout>

every answer is welcome,
Thanks in advance.

EDIT:
That works on the emulator with same o.s. version and Api.
So is this a problem of my phone or what? Maybe some security problem ?

1 Answers

Can you access the web page from your device using Chrome? If your server is running on your machine, both the devices have to be in the same network.

I also suggest that you add some useful logging to your catch clause, and that you use the DownloadStringAsync method instead of the synchronous one, or use the newer API HttpClient

Related