0

im working on posting a form to google server and getting the response in the form of html string and finally i put that string on webview to display result....i use the async task for this, progress dialog it shows but some times it shows me message to "force to close" without any changes i do in to code....that means prediction about output is unexpected... My code is like this......

public class Urlasync extends Activity {  
   WebView engine=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
        new AddStringTask().execute();
    }

   class AddStringTask extends AsyncTask<Void, Void, HttpResponse>
    {
        HttpResponse end = null;
        String endResult = null;
        public static  final int TIMEOUT_MS=10000;
         HttpClient client=null;
         HttpPost post =null;
         List<NameValuePair> pairs=null;
         BasicResponseHandler myHandler=null;
         private final ProgressDialog dialog = new ProgressDialog(Urlasync.this);
        @Override
        protected void onPreExecute() {
             client = new DefaultHttpClient();
             HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT_MS);
             HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT_MS);
             post = new HttpPost("http://www.google.com/m");
             pairs = new ArrayList<NameValuePair>();
             pairs.add(new BasicNameValuePair("hl", "en"));
             pairs.add(new BasicNameValuePair("gl", "us"));
             pairs.add(new BasicNameValuePair("source", "android-launcher-widget"));
             pairs.add(new BasicNameValuePair("q", "persistent"));
             try {
                    post.setEntity(new UrlEncodedFormEntity(pairs));
                    SystemClock.sleep(400);
        } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
             this.dialog.setMessage("starts...");
             this.dialog.show();

          }
        @Override
        protected HttpResponse doInBackground(Void... arg0) {

            try {
            HttpResponse response = client.execute(post);
                    if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
         {
             return response;
         }
             end = response;
            } catch (ClientProtocolException e) {
                        e.printStackTrace();
            } catch (IOException e) {   //this exception is called
                e.printStackTrace();
            }
        return end;

        }
        @Override       
        protected void onPostExecute(HttpResponse params) {
             if (this.dialog.isShowing()) {
                            this.dialog.dismiss();
                             }
                    if(params!=null)
         {
            String endResult=null;
            BasicResponseHandler myHandler = new BasicResponseHandler();
                try {
                    endResult = myHandler.handleResponse(params);

                } catch (HttpResponseException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
              engine = (WebView)findViewById(R.id.webview);
          engine.loadDataWithBaseURL("http://", endResult, "text/html", "UTF-8", null);
                setContentView(R.layout.main);
                 engine.requestFocus(View.FOCUS_DOWN);
                      }
             Toast.makeText(Urlasync.this, "Done!", Toast.LENGTH_SHORT).show();
        }
    }

    }

suggestions with code are welcome thank you..

10
  • 2
    What exception does occur? Please add the output of the log cat. Commented Apr 13, 2011 at 9:15
  • it says me to "force close".. Commented Apr 13, 2011 at 9:21
  • it says me that application stops unexpectedly.. Commented Apr 13, 2011 at 9:22
  • 1
    Read about LogCat, here you can find a cause of your problem Commented Apr 13, 2011 at 9:24
  • exception is NullPointerException ..at org.apache.http.impl.client.BasicResponceHandler.handleresponce Commented Apr 13, 2011 at 9:43

1 Answer 1

3

You shan't do anything with UI in doInBackground (like showing Toast). Do it in onPostExecute or in onProgressUpdate instead.

Sign up to request clarification or add additional context in comments.

9 Comments

in doInbackground i get the Httpresponce then in onPostExecute i parse it,show toast ans display that responce in webview... but i read some where that for async task require UI thread, so where i put that one..
Remove your showing toast from doInBackground or move it to onPostExecute.
exception is NullPointerException ..at org.apache.http.impl.client.BasicResponceHandler.handleresponce
You do it in doInBackground too, but you shan't: protected HttpResponse doInBackground(Void... arg0) { ... Toast.makeText(Urlasync.this, "Done execute!", Toast.LENGTH_SHORT).show(); ...
Debug onPostExecute and find that params is null
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.