Send http post from android
Question: I have a server made in python that reads the querystring-message and stores
it in a sqlite database, and then displays the content.

Now I want to send the message from a android application. This is my code so
far.

    
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.UnsupportedEncodingException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLEncoder;
    
    import android.R.string;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
    Button send;
    TextView display;
    String message;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        send = (Button)findViewById(R.id.button1);
        display = (TextView)findViewById(R.id.editText1);
    
    
        send.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                try{
                    post();
                }
                catch(Exception e)
                {
                    display.setText("Det sket sig");
    
                }
    
            }
    
            public void post() throws  UnsupportedEncodingException
            {
                message = display.getText().toString();
                String data = URLEncoder.encode("?message", "UTF-8") 
                        + "=" + URLEncoder.encode(message, "UTF-8");
    
                String text = "";
                BufferedReader reader=null;
    
                try
                {
                    URL url = new URL("http:homepage.net");
    
                    URLConnection conn = url.openConnection(); 
                    conn.setDoOutput(true); 
                    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
                    wr.write(data); 
                    wr.flush(); 
    
                    reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    StringBuilder sb = new StringBuilder();
                    String line = null;
    
                    while((line = reader.readLine()) != null)
                    {
                           sb.append(line + "\n");
                    }
                    text = sb.toString();
                }
                catch(Exception e)
                {
    
                }
                finally
                {
                    try
                    {
                        reader.close();
                    }
    
                    catch(Exception ex) {}
                }
                display.setText(text);
            }
        });
    
    
    }
    
    @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;
    }
    

}

This is not functioning as per the expectations. What am i doing wrong here?


Answer: for network related operation you have to use asynctask or thread other wise
you wil get NetworkOnMainThread Exception. refer
[here](http://developer.android.com/reference/android/os/AsyncTask.html)

    
    
    private class MyTask extends AsyncTask<Void, Void, Void> { ... }
    

