Note that there are some explanatory texts on larger screens.

plurals
  1. POprobleme with AsyncTask: execute
    primarykey
    data
    text
    <p>I tried to use <code>AsyncTask</code> object to connect my application to a remote server. Here's my code:</p> <pre><code>public class ConnectServer extends AsyncTask&lt;Void, Void, Void&gt; { String IP = ""; InputStream is = null; JSONObject json_data = null; ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(); ArrayList&lt;String&gt; donnees = new ArrayList&lt;String&gt;(); @Override protected Void doInBackground( Void... params ) { // TODO Auto-generated method stub IP = "http://192.168.101.1/fichier.php"; nameValuePairs.add( new BasicNameValuePair( "nom", envois.nom ) ); nameValuePairs.add( new BasicNameValuePair( "prenom", envois.prenom ) ); nameValuePairs.add( new BasicNameValuePair( "nationalite", envois.nationalite ) ); nameValuePairs.add( new BasicNameValuePair( "passeport", envois.passeport ) ); try { //commandes httpClient HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost( IP ); httppost.setEntity( new UrlEncodedFormEntity( nameValuePairs ) ); HttpResponse response = httpclient.execute( httppost ); HttpEntity entity = response.getEntity(); is = entity.getContent(); Log.i( "tag", "depuis json" ); } catch (Exception e) { Log.i( "taghttppost", "" + e.toString() ); Toast.makeText( c, e.toString(), Toast.LENGTH_LONG ).show(); } return null; } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); } @Override protected void onPostExecute( Void result ) { // TODO Auto-generated method stub super.onPostExecute( result ); Toast.makeText( getApplicationContext(), "fin d'envoi", Toast.LENGTH_SHORT ).show(); } } </code></pre> <p>Then when i try to call start that task, in my <code>onClick()</code> method, like this:</p> <pre><code>public void onClick( View v ) { // TODO Auto-generated method stub ConnectServer cs = new ConnectServer(); cs.execute( (Void) null ); } </code></pre> <p>It doesn't work. I debug it with eclipse, i am sure that the error come from this line:</p> <pre><code>cs.execute((Void)null); </code></pre> <p>I try to replace it by </p> <pre><code>cs.execute(); </code></pre> <p>But the error persists. The debbuger gives me something like:</p> <pre><code>Thread [&lt;10&gt; AsyncTask #1] (Suspended (exception RuntimeException)) ThreadPoolExecutor.runWorker(ThreadPoolExecutor$Worker) line: 1086 ThreadPoolExecutor$Worker.run() line: 561 Thread.run() line: 1096 Thread [&lt;12&gt; AsyncTask #2] (Running) </code></pre> <p>I want to add that my code works perfectly when I use it without <code>AsyncTask</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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