2

I am developing an Android application and when it launches :

1) I make an HTTP Request to my server to send a small JSON file.

2) Open a webView to show a URL.

When the server is running properly there is absolutely no problem and everything goes smoothly.

HOWEVER , if for any reason the server is down , the HTTP request literally hangs and i need to wait till there is an HTTP timeOut which is around 30seconds till i actually see the webView with the URL loading.

I read that i shouldn't make any networking inside the UI thread and i should use another thread for that.

In BlackBerry , that i have already developed the application for , this is as simple as that :

new Thread(){
  public void run(){
    HttpConnection hc = 
            (HttpConnection)Connector.open("http://www.stackoverflow.com");
  }
}.start();

I just start a new thread and inside i make the requests and all the necessary networking. That way , even when my server is not reachable the webView is loaded immediately without making the user wait and sense that the app is actually hanging.

How could i do exactly the same in Android , easiest way possible ?

donparalias
  • 1,834
  • 16
  • 37
  • 60

3 Answers3

7

Why not to use the same method as you use it for BlackBerry?

new Thread() {
  public void run() {
    new URL("http://www.stackoverflow.com").getContent();
  }
}.start();
MatrixDev
  • 1,432
  • 15
  • 20
5

Use AsyncTask, it's the simplest way to do that. For more details: http://developer.android.com/reference/android/os/AsyncTask.html

cy3er
  • 1,699
  • 11
  • 14
1

In icecream sandwich and above you are not allowed to use any web cal in UI thread. However you may use threads, but best way proposed by android is to use Async task. A very simple example is as follow.

"AsyncTask < Parameters Type, Progress Value Type, Return Type >"

   class MyAsyncTask extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
     // Runs on UI thread- Any code you wants 
     // to execute before web service call. Put it here.
     // Eg show progress dialog
    }
    @Override
    protected String doInBackground(String... params) {
      // Runs in background thread
        String result = //your web service request;         

         return result;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String resp) {

      // runs in UI thread - You may do what you want with response 
      // Eg Cancel progress dialog - Use result         
    }

}
Vadym
  • 163
  • 1
  • 4
  • 19
Adnan
  • 5,025
  • 5
  • 25
  • 44
  • Who told you that you cant use web calls in UI thread? And what exactly do mean by that? I just tested it on 4.1.2 and it worked fine – donparalias Apr 25 '13 at 11:33
  • I mean you can not directly call a web service but in thread. Eg. You can't call web service directly in any UI method. You must create a thread inside that method or to use asynctask. – Adnan Apr 25 '13 at 12:06
  • For future readers, networking on main is explicitly blocked and you have to override it when doing debugging (before writing the threading code). So, it is effectively a hard requirement to thread it now. I should mention AsyncTask is also deprecated now, and as of the time of writing, the AsyncTask documentation suggests java.util.concurrent. – Alexis Evelyn Apr 11 '21 at 05:28