Note that there are some explanatory texts on larger screens.

plurals
  1. POImplemented Async Task - App Force Closes when attempting to Parse Data (async task not executing?)
    text
    copied!<p>I'm very new to this - I added some source code to implement AsyncTask but it's still force closing due to running on the IO thread instead of implementing AsyncTask. I know I'm doing something incorrect but I'm not sure what it might be. </p> <p>JAVA:</p> <pre><code>import java.io.IOException; import org.apache.http.client.ClientProtocolException; import java.util.ArrayList; import android.os.AsyncTask; import java.util.List; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.message.BasicNameValuePair; // import everything you need import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class DeviceConfig extends Activity { AsyncTask&lt;Object, Object, Object&gt; saveContactTask = new AsyncTask&lt;Object, Object, Object&gt;() { @Override protected Object doInBackground(Object... params) { return null; } @Override protected void onPostExecute(Object result) { finish(); } }; Button sendButton; EditText msgTextField; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // load the layout setContentView(R.layout.deviceconfig); // make message text field object msgTextField = (EditText) findViewById(R.id.msgTextField); // make send button object sendButton = (Button) findViewById(R.id.sendButton); } // this is the function that gets called when you click the button public void send(View v) { // get the message from the message text box String msg = msgTextField.getText().toString(); // make sure the fields are not empty if (msg.length()&gt;0) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://gamedemo.hostzi.com/test.php"); try { List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(2); nameValuePairs.add(new BasicNameValuePair("id", "12345")); nameValuePairs.add(new BasicNameValuePair("message", msg)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); httpclient.execute(httppost); msgTextField.setText(""); // clear text box } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } } else { // display message if text fields are empty Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show(); } } } </code></pre> <p>XML:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;TextView android:text="Message" android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;EditText android:id="@+id/msgTextField" android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;Button android:text="Send" android:id="@+id/sendButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="send" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>PHP:</p> <pre><code>&lt;?php // get the "message" variable from the post request // this is the data coming from the Android app $message=$_POST["message"]; // specify the file where we will save the contents of the variable message $filename="androidmessages.html"; // write (append) the data to the file file_put_contents($filename,$message."&lt;br /&gt;",FILE_APPEND); // load the contents of the file to a variable $androidmessages=file_get_contents($filename); // display the contents of the variable (which has the contents of the file) echo $androidmessages; ?&gt; </code></pre> <p>LOGCAT:</p> <pre><code> 03-27 16:07:05.795: D/dalvikvm(11620): Late-enabling CheckJNI 03-27 16:07:05.935: D/dalvikvm(11620): GC_FOR_ALLOC freed 88K, 3% free 7355K/7568K, paused 18ms, total 19ms 03-27 16:07:05.945: I/dalvikvm-heap(11620): Grow heap (frag case) to 10.818MB for 3686416-byte allocation 03-27 16:07:05.975: D/dalvikvm(11620): GC_FOR_ALLOC freed 1K, 2% free 10953K/11172K, paused 25ms, total 25ms 03-27 16:07:05.995: D/dalvikvm(11620): GC_CONCURRENT freed &lt;1K, 2% free 10953K/11172K, paused 3ms+2ms, total 18ms 03-27 16:07:06.285: D/dalvikvm(11620): GC_FOR_ALLOC freed &lt;1K, 2% free 10953K/11172K, paused 15ms, total 16ms 03-27 16:07:06.305: I/dalvikvm-heap(11620): Grow heap (frag case) to 17.043MB for 6529744-byte allocation 03-27 16:07:06.315: D/dalvikvm(11620): GC_FOR_ALLOC freed 0K, 2% free 17330K/17552K, paused 16ms, total 16ms 03-27 16:07:06.335: D/dalvikvm(11620): GC_CONCURRENT freed &lt;1K, 2% free 17330K/17552K, paused 3ms+2ms, total 21ms 03-27 16:07:06.435: D/libEGL(11620): loaded /system/lib/egl/libEGL_tegra.so 03-27 16:07:06.455: D/libEGL(11620): loaded /system/lib/egl/libGLESv1_CM_tegra.so 03-27 16:07:06.465: D/libEGL(11620): loaded /system/lib/egl/libGLESv2_tegra.so 03-27 16:07:06.485: D/OpenGLRenderer(11620): Enabling debug mode 0 03-27 16:07:19.615: W/IInputConnectionWrapper(11620): showStatusIcon on inactive InputConnection 03-27 16:07:45.695: D/AndroidRuntime(11620): Shutting down VM 03-27 16:07:45.695: W/dalvikvm(11620): threadid=1: thread exiting with uncaught exception (group=0x41cd2930) 03-27 16:07:45.695: E/AndroidRuntime(11620): FATAL EXCEPTION: main 03-27 16:07:45.695: E/AndroidRuntime(11620): java.lang.IllegalStateException: Could not execute method of the activity 03-27 16:07:45.695: E/AndroidRuntime(11620): at android.view.View$1.onClick(View.java:3599) 03-27 16:07:45.695: E/AndroidRuntime(11620): at android.view.View.performClick(View.java:4204) 03-27 16:07:45.695: E/AndroidRuntime(11620): at android.view.View$PerformClick.run(View.java:17355) 03-27 16:07:45.695: E/AndroidRuntime(11620): at android.os.Handler.handleCallback(Handler.java:725) 03-27 16:07:45.695: E/AndroidRuntime(11620): at android.os.Handler.dispatchMessage(Handler.java:92) 03-27 16:07:45.695: E/AndroidRuntime(11620): at android.os.Looper.loop(Looper.java:137) 03-27 16:07:45.695: E/AndroidRuntime(11620): at android.app.ActivityThread.main(ActivityThread.java:5041) 03-27 16:07:45.695: E/AndroidRuntime(11620): at java.lang.reflect.Method.invokeNative(Native Method) 03-27 16:07:45.695: E/AndroidRuntime(11620): at java.lang.reflect.Method.invoke(Method.java:511) 03-27 16:07:45.695: E/AndroidRuntime(11620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 03-27 16:07:45.695: E/AndroidRuntime(11620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 03-27 16:07:45.695: E/AndroidRuntime(11620): at dalvik.system.NativeStart.main(Native Method) 03-27 16:07:45.695: E/AndroidRuntime(11620): Caused by: java.lang.reflect.InvocationTargetException 03-27 16:07:45.695: E/AndroidRuntime(11620): at java.lang.reflect.Method.invokeNative(Native Method) 03-27 16:07:45.695: E/AndroidRuntime(11620): at java.lang.reflect.Method.invoke(Method.java:511) 03-27 16:07:45.695: E/AndroidRuntime(11620): at android.view.View$1.onClick(View.java:3594) 03-27 16:07:45.695: E/AndroidRuntime(11620): ... 11 more 03-27 16:07:45.695: E/AndroidRuntime(11620): Caused by: android.os.NetworkOnMainThreadException 03-27 16:07:45.695: E/AndroidRuntime(11620): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 03-27 16:07:45.695: E/AndroidRuntime(11620): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 03-27 16:07:45.695: E/AndroidRuntime(11620): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 03-27 16:07:45.695: E/AndroidRuntime(11620): at java.net.InetAddress.getAllByName(InetAddress.java:214) 03-27 16:07:45.695: E/AndroidRuntime(11620): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 03-27 16:07:45.695: E/AndroidRuntime(11620): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 03-27 16:07:45.695: E/AndroidRuntime(11620): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 03-27 16:07:45.695: E/AndroidRuntime(11620): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 03-27 16:07:45.695: E/AndroidRuntime(11620): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 03-27 16:07:45.695: E/AndroidRuntime(11620): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 03-27 16:07:45.695: E/AndroidRuntime(11620): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 03-27 16:07:45.695: E/AndroidRuntime(11620): at com.nfc.linkingmanager.DeviceConfig.send(DeviceConfig.java:84) 03-27 16:07:45.695: E/AndroidRuntime(11620): ... 14 more </code></pre>
 

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