-1

I have the following code that suppose used to Send Data to Server using android App, but I keep getting error state that "unfortunately My app Name has been stopped"

My Activity Code is :

public class SendDataToServerActivity extends Activity {
    /** Called when the activity is first created. */
    Button sendButton;
    EditText msgTextField;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // make message text field object
        msgTextField = (EditText) findViewById(R.id.msgTextField);
        // make send button object
        sendButton = (Button) findViewById(R.id.sendButton);
        sendButton.setOnClickListener(new Button.OnClickListener(){
            //perform your action here            
            public void onClick(View v){
                String msg = msgTextField.getText().toString();  

                // make sure the fields are not empty
                if (msg.length()>0)
                {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://98.131.137.4/husam.php");
                 try {
                   List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                   nameValuePairs.add(new BasicNameValuePair("id", "12345"));
                   nameValuePairs.add(new BasicNameValuePair("message", msg));
                   httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                   httpclient.execute(httppost);
                   msgTextField.setText("");
                 } 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();
                }
            }
        });
    }
}

and my logs are as followed:

04-19 01:17:02.168: E/AndroidRuntime(583): FATAL EXCEPTION: main
04-19 01:17:02.168: E/AndroidRuntime(583): android.os.NetworkOnMainThreadException
04-19 01:17:02.168: E/AndroidRuntime(583):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
04-19 01:17:02.168: E/AndroidRuntime(583):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
04-19 01:17:02.168: E/AndroidRuntime(583):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
04-19 01:17:02.168: E/AndroidRuntime(583):  at libcore.io.IoBridge.connect(IoBridge.java:112)
04-19 01:17:02.168: E/AndroidRuntime(583):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-19 01:17:02.168: E/AndroidRuntime(583):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
04-19 01:17:02.168: E/AndroidRuntime(583):  at java.net.Socket.connect(Socket.java:842)
04-19 01:17:02.168: E/AndroidRuntime(583):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
04-19 01:17:02.168: E/AndroidRuntime(583):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
04-19 01:17:02.168: E/AndroidRuntime(583):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-19 01:17:02.168: E/AndroidRuntime(583):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-19 01:17:02.168: E/AndroidRuntime(583):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-19 01:17:02.168: E/AndroidRuntime(583):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-19 01:17:02.168: E/AndroidRuntime(583):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-19 01:17:02.168: E/AndroidRuntime(583):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-19 01:17:02.168: E/AndroidRuntime(583):  at com.husamalahmadi.send.SendDataToServerActivity$1.onClick(SendDataToServerActivity.java:55)
04-19 01:17:02.168: E/AndroidRuntime(583):  at android.view.View.performClick(View.java:3511)
04-19 01:17:02.168: E/AndroidRuntime(583):  at android.view.View$PerformClick.run(View.java:14105)
04-19 01:17:02.168: E/AndroidRuntime(583):  at android.os.Handler.handleCallback(Handler.java:605)

Is there any one can help and tell me what I am doing wrong here, knowing the fact that I have no error or warning once I debug my app

regards

Blundell
  • 75,855
  • 30
  • 208
  • 233
Husam
  • 31
  • 1
  • 4
  • Have you log cat :-)....then post here – Sandip Armal Patil Apr 15 '12 at 08:52
  • odd usage of BasicNameValuePair – sschrass Apr 15 '12 at 09:59
  • Hi Every One: this is message I received from my log file " E/AndroidRuntime(611): java.lang.IllegalStateException: Could not find a method send(View) in the activity class com.husamalahmadi.send.SendDataToServerActivity for onClick handler on view class android.widget.Button with id 'sendButton' – Husam Apr 15 '12 at 23:08
  • Class: SendDataToServerActivity. Method : onClick(). line number 55. Error: `android.os.NetworkOnMainThreadException` . Don't do networking stuff of the main UI thread. use a worker thread. – Blundell Apr 16 '12 at 22:23
  • This'll shed some light: http://stackoverflow.com/a/5513623/413127 – Blundell Apr 16 '12 at 22:24

1 Answers1

0

According to my question that I posted above, I kind of found the problem and solve it from its root and that could not happen without the help that I got from Blundell who commented in my question and here is the probelm and the answer.

the problem in the previous code while it looks so good it was preforming some code which will last for 5-10 seconds in the main UI thread, in early realse of android this was OK but in lateset realse of android it is not. so I added this followig code to the main activity:

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()  
                    .detectDiskReads()  
                    .detectDiskWrites()  
                    .detectNetwork()   
                                    .problems  
                    .penaltyLog()  
                    .build());  
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()  
                    .detectLeakedSqlLiteObjects()  
                    .detectLeakedClosableObjects()  
                    .penaltyLog()  
                    .penaltyDeath()  
                    .build());  

this code is only recommended to be implemented in the development Phase , but for production code I recommend using another thread which is worker thread to do File processing and Networking Stuff or any other heavy processing code and provide the Main UI thread with update. and here is a good starting tutorile you can check Android Threads, Handlers and AsyncTask - Tutorial

Husam
  • 31
  • 1
  • 4