- Home /
Async Get Request Android?
Hello everyone, I need your help.
Simple task: when user push the button, we need to send GET request to server(response is not necessary). The next code is working on the computer, but is not working on the Android.
public void Send(string arg)
{
WebRequest request = WebRequest.Create("http://x.x.x.x/server/?value=" + arg);
DoWithResponse(request, (response) => {
var body = new StreamReader(response.GetResponseStream()).ReadToEnd();
Debug.Log(body);
});
}
void DoWithResponse(WebRequest request, Action<HttpWebResponse> responseAction)
{
Action wrapperAction = () =>
{
request.BeginGetResponse(new AsyncCallback((iar) =>
{
var response = (HttpWebResponse)((HttpWebRequest)iar.AsyncState).EndGetResponse(iar);
responseAction(response);
}), request);
};
wrapperAction.BeginInvoke(new AsyncCallback((iar) =>
{
var action = (Action)iar.AsyncState;
action.EndInvoke(iar);
}), wrapperAction);
}
How can I resolve this?
Answer by TreyH · Sep 25, 2018 at 07:23 PM
For Android and non-windows, you will want to use the Unity web request objects.
using System;
using UnityEngine;
using UnityEngine.Networking;
public class RequestExample : MonoBehaviour
{
void Start()
{
this.StartCoroutine(AsyncRequest("https://www.google.com", this.RequestCallback));
}
private IEnumerator AsyncRequest(string url, Action<UnityWebRequest> callback)
{
// Start with the default Get configuration
var request = UnityWebRequest.Get(url);
// Configure the request
request.SetRequestHeader("X-Some-Header", "Header Value");
// Yield during the request
yield return request.SendWebRequest();
// Use the callback
callback(request);
}
private void RequestCallback(UnityWebRequest request)
{
// Just print to the console
Debug.LogFormat("Response Code: {0}\nResponse Text: {1}", request.responseCode, request.downloadHandler.text);
}
}
So you want it to be a synchronous request on mobile? What are you asking and what have you tried?
The case:
I have an app for Android. This app needs to make a query on server table at start. I need the app is shown and once i got the query result, show it. No problem about the time spent in query.
When i try with coroutine, the app is waiting to have the result with white screen (in my case is the background for splash) and once have the result, shows the first screen of my app.
After lot of work, i got a way in order the app opens and do the stuff after shown the first screen of app. With this, the app is opened and when i got the result, it is shown:
public async void codeCheck()
{
await Task.Run(() =>
{
//do the stuff//
});
}
This system is working for me, but... i can't use it with "old" webservices system, as once i update to .NET 4.x webservices doesn't work anymore. Then, i'm trying to use your system to get some data from DB. And i have the problem explained. If there is anyway to use the code show with yours, i think will be perfect!
Answer by PeterPam · Sep 26, 2018 at 02:17 PM
@TreyH How to proceed if i do not want to wait for response from server? i mean if i want to do "write-row" async without wait for result and continue execution. Thanks in advance.
That is how the example works. You aren't waiting for anything, you call the request as a coroutine then go about your business. Nothing is held up or waiting on a response. When the response arrives, it will be available through the callback.
To see this, use this for your Start function in that example:
void Start()
{
// This starts an async process
this.StartCoroutine(AsyncRequest("https://www.google.com", this.RequestCallback));
// This will be called right after that
Debug.Log("I was called without waiting for that request!");
}
Your answer
Follow this Question
Related Questions
I cant get response json from rest call in Unity 1 Answer
Facebook app invitation in android 0 Answers
Unity freezes when adding Task to List 1 Answer
Facebook app invitation in android -1 Answers