0

I have no errors or warnings in Eclipse and I'm completely new to Android Programming so I don't even know where to start with this.

My app is just a simple form that I need to post to a php script online.

Here's the bulk of my main Activity minus the imports.. I don't have it set up to do anything with return values or any of that, and TBH i don't even know what would happen if it did work other than the data would be in my DB.. but the PHP script is not even being called by my app at all.

Based on things I found in Google, I have tried -Adding Support Libraries -Changing the target sdk version from 19 to 18

Please, what am I doing wrong?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_apply);

    subm = (Button) findViewById(R.id.button1);
    fname = (EditText) findViewById(R.id.editText1);
    lname = (EditText) findViewById(R.id.editText2);
    addr = (EditText) findViewById(R.id.editText3);
    city = (EditText) findViewById(R.id.editText4);
    state = (EditText) findViewById(R.id.editText5);
    zip = (EditText) findViewById(R.id.editText6);
    phone = (EditText) findViewById(R.id.editText7);
    dob = (EditText) findViewById(R.id.editText8);
    email = (EditText) findViewById(R.id.editText9);
    ssn = (EditText) findViewById(R.id.editText10);

    subm.setOnClickListener(
    new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            try{

                String httpsURL = "https://example.com/apis/submit_credit_application.php";

                String query = "fname="+URLEncoder.encode(fname.getText().toString(),"UTF-8"); 
                query += "&lname="+URLEncoder.encode(lname.getText().toString(),"UTF-8");
                query += "&addr="+URLEncoder.encode(addr.getText().toString(),"UTF-8");
                query += "&city="+URLEncoder.encode(city.getText().toString(),"UTF-8");
                query += "&state="+URLEncoder.encode(state.getText().toString(),"UTF-8");
                query += "&zip="+URLEncoder.encode(zip.getText().toString(),"UTF-8");
                query += "&phone="+URLEncoder.encode(phone.getText().toString(),"UTF-8");
                query += "&dob="+URLEncoder.encode(dob.getText().toString(),"UTF-8");
                query += "&email="+URLEncoder.encode(email.getText().toString(),"UTF-8");
                query += "&ssn="+URLEncoder.encode(ssn.getText().toString(),"UTF-8");

                URL myurl = new URL(httpsURL);
                HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
                con.setRequestMethod("POST");

                con.setRequestProperty("Content-length", String.valueOf(query.length())); 
                con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");  
                con.setDoOutput(true); 
                con.setDoInput(true); 

                DataOutputStream output = new DataOutputStream(con.getOutputStream());  
                output.writeBytes(query);
                output.close();


            }catch(IOException e){

                Toast.makeText(
                    getApplicationContext(),
                    (CharSequence) e, 
                    Toast.LENGTH_LONG
                ).show();

            }

        }
    });
}

here's a Pastebin with my logcat

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116

2 Answers2

1

Alright! since you want you two points. Here is a good tutorial where i call a json web service using AsyncTask in andorid. Basically AsyncTask creates a new thread where network operations can be conducted.

Peshal
  • 1,508
  • 1
  • 12
  • 22
1

As your error says you can't run network tasks on the the main thread. AsyncTasks are good for running short tasks that you don't want to block the main thread with. Link to google docs. http://developer.android.com/reference/android/os/AsyncTask.html

// This class is just added somewhere in your main activity, like a function.
 private class PostFormTask extends AsyncTask<String, Integer, Long> {

     protected Long doInBackground(String... queryDetails) {

     try{

            String httpsURL = "https://example.com/apis/submit_credit_application.php";

            String query = "fname="+URLEncoder.encode(queryDetails[0],"UTF-8"); 
            query += "&lname="+URLEncoder.encode(queryDetails[1],"UTF-8");
            query += "&addr="+URLEncoder.encode(queryDetails[2],"UTF-8");
            // Keep adding to your query but instead of getting your details
            // from the textview they are in the queryDetails array.


            URL myurl = new URL(httpsURL);
            HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
            con.setRequestMethod("POST");

            con.setRequestProperty("Content-length", String.valueOf(query.length())); 
            con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");  
            con.setDoOutput(true); 
            con.setDoInput(true); 

            DataOutputStream output = new DataOutputStream(con.getOutputStream());  
            output.writeBytes(query);
            output.close();


        }catch(IOException e){

            Toast.makeText(
                getApplicationContext(),
                (CharSequence) e, 
                Toast.LENGTH_LONG
            ).show();

        }
         return false
     }

     protected void onPostExecute(Long result) {

     }
 }

Then on your onClick event just have.

new PostFormTask().execute(fname.getText().toString(),
                                    lname.getText().toString() );
// just send your form details to your task here, you will want to add all your details 
// from your above code.

Hope that helps.

Rory O'Logan
  • 181
  • 1
  • 6