user3780814
user3780814

Reputation: 147

Android app crashes on AsyncTask for http post to a server

i'm trying to test the AsyncTask and for that i used this tutorial http://mobiledevtuts.com/android/android-http-with-asynctask-example/

The code looks like this

package com.example.httpclient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.opengl.Visibility;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

private EditText value;
private Button btn;
private ProgressBar pb;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    value=(EditText)findViewById(R.id.editText1);
    btn=(Button)findViewById(R.id.button1);
    pb=(ProgressBar)findViewById(R.id.progressBar1);
    pb.setVisibility(View.GONE);
    btn.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    if(value.getText().toString().length()<1){
    // out of range
        Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();
    }else{
        pb.setVisibility(View.VISIBLE);
            AsyncTask task = new MyAsyncTask();
            task.execute(value.getText().toString());


    }

}

private class MyAsyncTask extends AsyncTask<String, Integer, Double>{

@Override
protected Double doInBackground(String... params) {
    // TODO Auto-generated method stub
    postData(params[0]);
    return null;
}

protected void onPostExecute(Double result){
    pb.setVisibility(View.GONE);
    Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
}
protected void onProgressUpdate(Integer... progress){
    pb.setProgress(progress[0]);
}

public void postData(String valueIWantToSend) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("myurl...");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
}
}

}
}

LogCat looks like this:

10-10 17:22:09.755: D/ProgressBar(32647): setProgress = 0
10-10 17:22:09.755: D/ProgressBar(32647): setProgress = 0, fromUser = false
10-10 17:22:09.755: D/ProgressBar(32647): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 100
10-10 17:22:09.895: D/libEGL(32647): loaded /system/lib/egl/libEGL_mali.so
10-10 17:22:09.905: D/libEGL(32647): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-10 17:22:09.910: D/libEGL(32647): loaded /system/lib/egl/libGLESv2_mali.so
10-10 17:22:09.915: E/(32647): Device driver API match
10-10 17:22:09.915: E/(32647): Device driver API version: 23
10-10 17:22:09.915: E/(32647): User space API version: 23 
10-10 17:22:09.915: E/(32647): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct  9 21:05:57 KST 2013 
10-10 17:22:10.035: D/OpenGLRenderer(32647): Enabling debug mode 0
10-10 17:22:14.050: D/ProgressBar(32647): updateDrawableBounds: left = 0
10-10 17:22:14.050: D/ProgressBar(32647): updateDrawableBounds: top = 0
10-10 17:22:14.050: D/ProgressBar(32647): updateDrawableBounds: right = 152
10-10 17:22:14.050: D/ProgressBar(32647): updateDrawableBounds: bottom = 152
10-10 17:22:14.120: W/dalvikvm(32647): threadid=11: thread exiting with uncaught exception (group=0x41ba6700)
10-10 17:22:14.190: E/AndroidRuntime(32647): FATAL EXCEPTION: AsyncTask #1
10-10 17:22:14.190: E/AndroidRuntime(32647): java.lang.RuntimeException: An error occured while executing doInBackground()
10-10 17:22:14.190: E/AndroidRuntime(32647):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-10 17:22:14.190: E/AndroidRuntime(32647):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-10 17:22:14.190: E/AndroidRuntime(32647):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-10 17:22:14.190: E/AndroidRuntime(32647):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-10 17:22:14.190: E/AndroidRuntime(32647):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-10 17:22:14.190: E/AndroidRuntime(32647):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-10 17:22:14.190: E/AndroidRuntime(32647):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-10 17:22:14.190: E/AndroidRuntime(32647):    at java.lang.Thread.run(Thread.java:841)
10-10 17:22:14.190: E/AndroidRuntime(32647): Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]
10-10 17:22:14.190: E/AndroidRuntime(32647):    at com.example.httpclient.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:1)
10-10 17:22:14.190: E/AndroidRuntime(32647):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-10 17:22:14.190: E/AndroidRuntime(32647):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-10 17:22:14.190: E/AndroidRuntime(32647):    ... 4 more
10-10 17:22:24.550: I/Choreographer(32647): Skipped 610 frames!  The application may be doing too much work on its main thread.
10-10 17:22:26.085: I/Process(32647): Sending signal. PID: 32647 SIG: 9

i also had the same problem when testing another tutorial with async tasks.

Upvotes: 1

Views: 573

Answers (1)

ToYonos
ToYonos

Reputation: 16833

Change the declaration of your task :

MyAsyncTask task = new MyAsyncTask();

If you declare it as an AsyncTask, it means you declare it as AsyncTask<Object, Object, Object>. Hence, doInBackground thinks it will receive an Object array, and it received a String array, that explains the ClassCastException

Upvotes: 1

Related Questions