Results 1 to 6 of 6
  1. #1

    how to debug runtime exception in AsyncTask?

    I am a relatively new to Android development, so pls, bear with me.

    I took a post from mobiledevtuts android/android-http-with-asynctask-example/
    xml and php info is also there.
    I/ve added 2 line to the xml:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_ST ATE"/>

    The app dies after httpclient.execute()
    Any ideas?
    ----------------------------------------------------------------------------------------------------------------------------------

    Below is my code from the activity.java:

    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.support.v7.app.ActionBarActivity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ProgressBar;
    import android.widget.Toast;

    public class MainActivity extends ActionBarActivity implements OnClickListener {
    //http://mobiledevtuts.com/android/android-http-with-asynctask-example/
    private EditText value;
    private int lhError;
    private Button btn;
    private ProgressBar pb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    lhError = 0;
    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);
    lhError = 0;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    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);
    new MyAsyncTask().execute(value.getText().toString());
    }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
    return true;
    }
    return super.onOptionsItemSelected(item);
    }
    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("http://example.com/andr-test.php");

    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);
    lhError = 1;


    } catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    lhError = -2;
    } catch (IOException e) {
    // TODO Auto-generated catch block
    lhError = -3;

    }
    }

    }
    }

  2. #2
    please attach the log ,



  3. #3
    Hi,
    You want to send a value pair over http(s) and because you are using async task your debugger doesn't look inside the task?
    Could you give a little more info on what you want to do? Do you own/lease the server on the receiving side? Does the server give a standard answer to what you intend to do and if so do you need to capture the answer? Is the protocol only http or could it also be https? Does the server accept JSONObjects? Have you tried Log.d("TAG", variables) for logging inside the asynctask? Do you need feedback (progressbar) for a keypair value? And if not: Have you considered using the ExecutorService with a fixed threadpool and a callable with an arrayresult as you try to do with List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();??

    Please shed some light for us - eager to help you )))



  4. #4
    I just realized that this post is from may 2014. Gulp!



  5. #5
    Member
    Join Date
    Feb 2015
    Posts
    16

    Device(s)
    HTC Desire 820, Samsung Galaxy Note 3 Neo, Nexus 6, Micromax Canvas Knight
    Resume it with the Eclipse debugger.
    Read out the same problem mentioned here, may it will help. http://stackoverflow.com/questions/6...ndroid-eclipse



  6. #6
    Thank you, nz and m3r for your help. I think the "resume" probably would have worked at the time. But I gave up on that example, found another one,
    and made that work.



Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •