Why does android 2.1-update1 hang on HttpClient.execute() and not android 2.2? - android-2.2-froyo

I am writing an android app that makes requests using the HttpClient interface from the Apache Commons project (supplied with both versions of android).
The problem occurs in the following code listing:
try {
URI uri = URIUtils.createURI(SCHEME, host, DEFAULT_PORT, QUERY,
URLEncodedUtils.format(qparams, ENCODING), EMPTY_FRAGMENT);
HttpUriRequest request = new HttpGet(uri);
response = client.execute(request);
} catch (Exception e) {
throw new CheckedSecurityException("Could not execute request", e);
}
Android 2.2 does this just fine (API level 8) but when I run this on Android 2.1-update1 (API level 7) it "hangs" at client.execute(request). What am I doing wrong?

Okay, I figured this out myself.
Part of the problem was that I had to use 10.0.2.2 from the emulator and I wrote a class that detects the emulator using the model. the string google_sdk worked fine for me before. But yesterday I updated my SDK and apparently they changed it to just sdk so the class wasn't detecting the device as an emulator before and was using the wrong IP address.
Status: Solved!

Related

Xamarin.UITest Tests not working only on real Android devices

I'm new to XamarinUITest and I am having trouble running it on real devices locally (in preparation for AppCenter Test).
When I run it using an Android emulator, the test runs smoothly, passes and completes.
When I run it on a real device connected to my machine (OnePlus 5T) I get the following issues:
Message: 
System.Exception : Post to endpoint '/ping' failed after 100 retries. No http result received
Stack Trace: 
AppInitializer.StartApp(Platform platform) line 24
Login.BeforeEachTest() line 25
Standard Output: 
Full log file: C:\Users\Steve\AppData\Local\Temp\uitest\log-2021-11-25_13-54-49-953.txt
Skipping IDE integration as important properties are configured. To force IDE integration, add .PreferIdeSettings() to ConfigureApp.
Android test running Xamarin.UITest version: 3.2.3
Initializing Android app on device 3acaec7 with apk: C:\Users\Steve\source\repos\MyTestApp\MyTestApp.Android\bin\Dev\com.mytestapp.dev-Signed.apk
Skipping local screenshots. Can be enabled with EnableScreenshots() when configuring app.
Signing apk with Xamarin keystore.
I have internet permissions on in my AndroidManifest.xml file.
AppInitializer
public static IApp StartApp(Platform platform)
{
try
{
if (platform == Platform.Android)
return ConfigureApp.Android.ApkFile(#"C:\Users\Steve\source\repos\MyTestApp\MyTestApp.Android\bin\Dev\com.mytestapp.dev-Signed.apk").StartApp();
else
return ConfigureApp.iOS.StartApp();
}
catch(Exception ex)
{
throw(ex);
}
}
I get this same issue if I try upload it to AppCenter. If I run the app when the APK file is already on the device then the test loading indicator keeps spinning forever.
I have tried .InstalledApp() instead and also had no luck on real devices. This is v important so I really appreciate everyone chimming in. Thanks.

Xamarin BluetoothLE Plugin not working

I created a new Xamarin.Forms app to test basic Bluetooth functionality. I downloaded this plugin into both the Android project and the shared project:
https://github.com/aritchie/bluetoothle
I wrote this function in the shared project and am calling it from the OnCreate of my launch activity in my Android project:
public static async Task BroadcastBluetooth()
{
// (I do not await this function when I call it)
try
{
await Task.Delay(5000); // just to make sure we give enough time for all initialization to complete
_server = CrossBleAdapter.Current.CreateGattServer();
// exception thrown on this line
await _server.Start(new AdvertisementData
{
LocalName = "TestServer",
ServiceUuids = new List<Guid>()
});
}
catch (Exception e)
{
}
}
It throws this exception:
{System.NullReferenceException: Object reference not set to an
instance of an object. at
Plugin.BluetoothLE.Server.GattServer.StartAdvertising
(Plugin.BluetoothLE.Server.AdvertisementData adData) [0x00095] in
C:\dev\acr\bluetoothle\Plugin.BluetoothLE.Android\Server\GattServer.cs:135
at Plugin.BluetoothLE.Server.GattServer.Start
(Plugin.BluetoothLE.Server.AdvertisementData adData) [0x00011] in
C:\dev\acr\bluetoothle\Plugin.BluetoothLE.Android\Server\GattServer.cs:70
at App2.App+d__7.MoveNext () [0x00097] in
C:\Projects\app2\App2\App2\App.xaml.cs:49 }
I'm only doing something really basic so I must be missing something? The exception is referencing a directory path of the plugin's developer's machine (C:\dev\acr...) so either this plugin is broken or I'm doing something really wrong?
I am using the same plugin in a current project of mine and it is working fine. So I doubt the issue is within the plugin.
Some thoughts about what could cause the issue:
Are you testing this code on a real device that is capable of performing BLE Advertisement?
Do you have set the permissions accordingly in your android project?
Does BLE Advertising work when you use the native android apis?
It also would be helpful, if you could attach a repository with which I can reproduce the issue.

iOS Httpclient cannot connect: {System.Threading.Tasks.TaskCanceledException}

I'm building an app with Xamarin Forms. I have no issues with Android, but when I attempt to simulate or deploy the iOS app, my HttpClient can't seem to connect to the server. After the timeout expires, I get a TaskCanceledException.
The HttpClient is actually used in a separate project that is referenced by the iOS app, if that matters. Here's my usage:
string serviceUri = service + methodName;
HttpRequestMessage request = new HttpRequestMessage(methodRequestType, serviceUri)
{
Content = new StringContent(content, Encoding.UTF8, "application/json")
};
HttpResponseMessage response = await _client.SendAsync(request).ConfigureAwait(false);
string returnString = await response.Content.ReadAsStringAsync();
return returnString;
I'm not using any waits or accessing .Result, as I've seen many people try that don't understand async operations.
I've seen older posts (circa 2013) where the wrong HttpClient is used. Is this still an issue in current releases? I've also tried changing the HttpClient implementation in the iOS project settings, to no avail.
Thanks in advance!
The issue was an invalid Deployment Target - It was set to "80" instead of "8.0
In come cases, the build server does not communicate this invalidity back to your machine - hence why it took so long to notice it!

Can't call Web api from Xamarin

I'm trying to make a simple web api call using HttpClient in an iOS build. When setting the client.BaseAddress, the BaseAddress always ends up null. I can't find what could possibly be wrong.
using System.Net.Http;
....
private const string BaseApiUrl = "http://localhost:55904/";
static HttpClient client = new HttpClient();
client.BaseAddress = new Uri(BaseApiUrl);
// client.BaseAddress is still null
I downloaded a sample non-Xamarin project and the same code works fine.
Any ideas?
Update
As I continue messing with this I figured I would try some of the other platforms. UWP seems to work fine. I was going to test Android but all of a sudden, I have a bunch of "The name 'InitializeComponent' does not exist in the current context" errors.
I'm new to Xamarin but not to VS or C#.
Update
I was just thinking, does iOS need to request any special permission for internet access? If so, where would that be set?
Just in case anyone else comes across this problem, I found the cause. It turns out that the iOS project didn't include a reference to System.Net.Http. Once I added the reference, everything worked fine.

Visual Studio android emulator httprequest fails

So I've been working on a xamarin PCL project targeting android and windows store app and I've had this issue for about two weeks now. One of the very first things this app does is to make an http request to a yahoo service when the user tries to search for something.
Now, on the windows store app project, this works just fine. However, whenever I'm debugging the android project, this fails miserably. It times out and I get a TaskAbortedException.
I've navigated with the browser within the android emulator to the restful service url and I do get a response in the browser but nothing when I make the http request. I have tried everything I could think of but no cigar. I have researched this for weeks now and I have yet to find an answer. It should be noted that I'm making the request within the PCL project with HttpClient.
Here's the code where this happens:
var queryUrl = string.Format(QUERY_URL_TEMPLATE, TickerSearch);
try
{
var requestTask = httpClient.GetStringAsync(queryUrl);
requestTask.ContinueWith(t =>
{
var responseDto = JsonConvert.DeserializeObject<TickerSearchResultDto>(t.Result);
TickerSearchResults = responseDto.ResultSet.Result;
});
}
catch (Exception ex)
{
}
Any help is greatly appreciated.

Resources