My android application crashes when trying to send data to a webserver -


i want learn how send data android application webserver. have copied code tutorial when application tries send data crashes. logcat says: fatalexception:main

i don't have webserver send data yet, don't think problem. application crashes before gets anything. before ask, yes have granted internet permissions.

the program following:

package com.example.helloworld;  import java.io.ioexception; import org.apache.http.client.clientprotocolexception;   import java.util.arraylist; 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 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 mainactivity extends activity {  button sendbutton;  edittext msgtextfield;  /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     // load layout     setcontentview(r.layout.activity_main);              // make message text field object     msgtextfield = (edittext) findviewbyid(r.id.msgtextfield);     // make send button object     sendbutton = (button) findviewbyid(r.id.sendbutton);  }  // function gets called when click button public void send(view v) {     // message message text box     string msg = msgtextfield.gettext().tostring();        // make sure fields not empty     if (msg.length()>0)     {         httpclient httpclient = new defaulthttpclient();         httppost httppost = new httppost("http://yourwebsite.com/yourphpscript.php");      try {        list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(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 empty         toast.maketext(getbasecontext(),"all field required",toast.length_short).show();     }  }  } 

this tutorial following:

http://webtutsdepot.com/2011/11/15/android-tutorial-how-to-post-data-from-an-android-app-to-a-website/

you can not web interaction in main thread, should in other thread.
can asynctask class net interactions

here simple example asynctask , internet reaction understanding asynctask – once , forever

see asynctask documentation

note app must have android.permission.internet permission


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -