0

I've been trying to send data from my android application to my server. The server I'm using is EasyPHP. In my onCreate() function, I have:

    user_editbox  = (EditText) findViewById(R.id.username_id);
    phone_editbox = (EditText) findViewById(R.id.phonenum_id);
    submit_button = (Button) findViewById(R.id.submit_id);



    submit_button.setOnClickListener( new View.OnClickListener() {

               String username = "Gina";
               String phone_number = "2225550000";
                    public void onClick(View view)
                    {


                        Log.d(TAG, username);
                        Log.d(TAG, phone_number);
                        if(username != null && phone_number != null)
                        {
                            Log.d(TAG, "Inside " + username);
                            Log.d(TAG, "Inside " + phone_number);

                            try {
                                Log.d(TAG, "Entered try");
                                URL url = new URL("http://localhost/Web%20/Practice/Android%20Login/phpserver.php");
                                Log.d(TAG, "URL given");
                                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                                urlConnection.setRequestMethod("POST");
                                urlConnection.setDoOutput(true);




                                Uri.Builder builder = new Uri.Builder()
                                        .appendQueryParameter("Username ", username)
                                        .appendQueryParameter("PhoneNumber ", phone_number );
                                String query = builder.build().getEncodedQuery();


                                OutputStream os = urlConnection.getOutputStream();    // <--- Problem
                                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                                writer.write(query);
                                writer.flush();
                                writer.close();
                                os.close();
                                urlConnection.connect();

                            }catch(IOException e)
                            {
                                e.printStackTrace();
                            }

The problem arises when the program reaches

        OutputStream os = urlConnection.getOutputStream();

A dialog box appears stating

   Unfortunately, Program has stopped  

I'm trying to understand what could cause the program to crash like this with the highlighted code. The server seems to be running smoothly and the PHP script appears like so:

Any ideas?

Logcat

   07-11 22:17:30.118 2645-2645/com.example.czar.connectingtoserver D/MSG: onCreate
   07-11 22:17:30.120 2645-2645/com.example.czar.connectingtoserver D/MSG: onStart
   07-11 22:17:30.122 2645-2645/com.example.czar.connectingtoserver D/MSG: onResume
   07-11 22:19:24.758 2645-2645/com.example.czar.connectingtoserver D/MSG: Clicked
   07-11 22:19:24.758 2645-2645/com.example.czar.connectingtoserver D/MSG: Gina
   07-11 22:19:24.758 2645-2645/com.example.czar.connectingtoserver D/MSG: 2225550000
   07-11 22:19:24.759 2645-2645/com.example.czar.connectingtoserver D/MSG: Inside Gina
   07-11 22:19:24.759 2645-2645/com.example.czar.connectingtoserver D/MSG: Inside 2225550000
   07-11 22:19:24.759 2645-2645/com.example.czar.connectingtoserver D/MSG: Entered try
   07-11 22:19:24.759 2645-2645/com.example.czar.connectingtoserver D/MSG: URL given
   07-11 22:19:24.759 2645-2645/com.example.czar.connectingtoserver D/MSG: Output set to true
   07-11 22:19:24.759 2645-2645/com.example.czar.connectingtoserver D/MSG: String Encoded

1 Answers1

0

The crashing because there is a NetworkOnMainThreadException here. You need put all work with HttpURLConnection to a Thread.

mdtuyen
  • 4,470
  • 5
  • 28
  • 50
  • I thought all programs usually have atleast one thread running? –  Jul 12 '16 at 02:28
  • In your case the block in onClick event cann't run on main thread, you need separate it to other thread. – mdtuyen Jul 12 '16 at 03:33