Note that there are some explanatory texts on larger screens.

plurals
  1. POError NetworkOnMainThreadException Consuming Restful Webservice using Android Client 4.0
    text
    copied!<p>i have problem regarding my android client.</p> <p>I have a script that consume a restful webservice and determine user authentication here's my code</p> <pre><code>import java.io.IOException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import android.os.AsyncTask; import android.util.Log; public class RestLogin { private static String URL = "http://REST Webservice here?"; private static final String tag = "Logcat tag: "; public static Boolean Auth(String username,String password) { String result = ""; HttpClient httpclient = new DefaultHttpClient(); HttpGet request = new HttpGet(URL); //adding parameter to request to determine user authentication request.addHeader("username",username); request.addHeader("password",password); //taking handler to get execution status ResponseHandler&lt;String&gt; handler = new BasicResponseHandler(); try { result = httpclient.execute(request, handler); } catch (ClientProtocolException cpe) { cpe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } catch(IllegalArgumentException iae) { iae.printStackTrace(); } catch(NullPointerException npe) { npe.printStackTrace(); } httpclient.getConnectionManager().shutdown(); Log.i(tag,result); return Boolean.parseBoolean(result); } </code></pre> <p>}</p> <p>First, when i build the code, it shows error NetworkOnMainThreadException on my logcat status. Then, i find that since google android honeycomb, i can't directly using this method. So i need to add AsyncTask&lt;>, here's the code</p> <pre><code>public class RestLogin extends AsyncTask&lt;String, String, Boolean&gt; { private static String URL = "http://Restful webservice here?"; private static final String tag = "Logcat tag: "; @Override protected Boolean doInBackground(String... params) { //TO DO Auto Generated Method return null; } </code></pre> <p>now i'm stuck, how i can call the Auth() method using AsyncTask? </p> <p>thanks in advance.</p> <p>okay, UPDATE: this is new syntax</p> <pre><code>public class RestLogin extends AsyncTask&lt;Void, Void, Void&gt; { private static String URL = "http://REST Webservice here?"; private static final String tag = "Logcat tag: "; private String usname, psswrd; public RestLogin(String username, String password){ usname = username; psswrd = password; } protected void onPreExecute(){ super.onPreExecute(); } protected Void doInBackground(Void... params) { Boolean stat = Auth(usname,psswrd); return null; } protected void onPostExecute(Void result) { super.onPostExecute(result); } public static Boolean Auth(String username,String password) { String result = ""; HttpClient httpclient = new DefaultHttpClient(); HttpGet request = new HttpGet(URL); //adding parameter to request to determine user authentication request.addHeader("username",username); request.addHeader("password",password); //taking handler to get execution status ResponseHandler&lt;String&gt; handler = new BasicResponseHandler(); try { result = httpclient.execute(request, handler); } catch (ClientProtocolException cpe) { cpe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } catch(IllegalArgumentException iae) { iae.printStackTrace(); } catch(NullPointerException npe) { npe.printStackTrace(); } httpclient.getConnectionManager().shutdown(); Log.i(tag,result); return Boolean.parseBoolean(result); } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload