Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by VINOKUR · May 16, 2015 at 03:50 AM · androidasyncgetrequest

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PeterPam · Sep 25, 2018 at 05:40 PM 0
Share

Any new about this?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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);
     }
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PeterPam · Sep 26, 2018 at 02:23 PM 0
Share

Run async in computer, but not on Android devices.

avatar image TreyH PeterPam · Sep 26, 2018 at 03:06 PM 0
Share

So you want it to be a synchronous request on mobile? What are you asking and what have you tried?

avatar image PeterPam TreyH · Sep 26, 2018 at 03:31 PM 0
Share

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!

Show more comments
Show more comments
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreyH · Sep 26, 2018 at 02:27 PM 0
Share

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.

avatar image TreyH · Sep 26, 2018 at 02:29 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

AsyncOperation.progress still broken? 4 Answers

See Request of WWW or UnityWebRequest, 403 error only from android in a specific url (GET) 2 Answers

Post Requests without Coroutines 2 Answers

Async parse.com query not working on Android/iOS 2 Answers

Why is loading addressables 20x slower for an Android app bundle? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges