0

I need to submit my registration form details to server api. I have tried one method by calling a function on button click.But nothing is geting posted in the server and I am also not getting any exception.Please help me. Thanks in advance. Below is the code .

I have called the function like this

 button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try{

                // CALL GetText method to make post method call
                GetText();
            }
            catch(Exception ex)
            {
                ex.printStackTrace();;
                Toast.makeText(getActivity(),"URL Exception", Toast.LENGTH_LONG).show();
            }

And below given is the function

  public String GetText()  throws UnsupportedEncodingException
{
    HttpURLConnection connection = null;
    // Get user defined values
    Name = full_name.getText().toString();
    Phonenumber = phone.getText().toString();
    Email=email.getText().toString();
    Password=password.getText().toString();
    House = house.getText().toString();
    Street = street.getText().toString();
    Landmark = landmark.getText().toString();
    // Create data variable for sent values to server
    String data = URLEncoder.encode("name", "UTF-8")
            + "=" + URLEncoder.encode(Name, "UTF-8");
    data += "&" + URLEncoder.encode("email", "UTF-8") + "="
            + URLEncoder.encode(Email, "UTF-8");
    data += "&" + URLEncoder.encode("password", "UTF-8") + "="
            + URLEncoder.encode(Password, "UTF-8");
    data += "&" + URLEncoder.encode("phone", "UTF-8")
            + "=" + URLEncoder.encode(Phonenumber, "UTF-8");
    data += "&" + URLEncoder.encode("house", "UTF-8")
        + "=" + URLEncoder.encode(House, "UTF-8");
    data += "&" + URLEncoder.encode("street", "UTF-8")
            + "=" + URLEncoder.encode(Street, "UTF-8");
    data += "&" + URLEncoder.encode("areaname", "UTF-8")
            + "=" + URLEncoder.encode(Area, "UTF-8");
    data += "&" + URLEncoder.encode("landmark", "UTF-8")
            + "=" + URLEncoder.encode(Landmark, "UTF-8");

    // Send data
    try
    {

        // Defined URL  where to send data
        URL url = new URL("http://application.easypani.com/app/customer/register");
        // Send POST data request
        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        connection.setRequestProperty("Content-Length", "" +
                Integer.toString(data.getBytes().length));
        connection.setRequestProperty("Content-Language", "en-US");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        //send request
        DataOutputStream wr = new DataOutputStream (
                connection.getOutputStream ());
        wr.writeBytes(data);
        wr.flush();
        wr.close();

        // Get the server response
        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();
        while((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
        }
        rd.close();
        return response.toString();

    } catch (Exception e) {

        e.printStackTrace();
        return null;

    } finally {

        if(connection != null) {
            connection.disconnect();
        }
    }
}
Amit Nair
  • 295
  • 2
  • 5
  • 21

1 Answers1

2

Use this instead

private class PostData extends AsyncTask < String, Void, Void > {
    protected Void doInBackground(String...params) {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://application.easypani.com/app/customer/register");

        try {
            // Add your data
            String Name = params[0];
            String Phonenumber = params[1];
            String Email = params[2];
            String Password = params[3];
            String House = params[4];
            String Street = params[5];
            String Landmark = params[6];

            List < NameValuePair > nameValuePairs = new ArrayList < NameValuePair > ();
            nameValuePairs.add(new BasicNameValuePair("name", Name));
            nameValuePairs.add(new BasicNameValuePair("email", Email));............................................................
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
        return null;
    }
}

And you use it like this

new PostData().execute(Name, PhoneNumber..and so on);
Randyka Yudhistira
  • 3,612
  • 1
  • 26
  • 41
  • Really sorry Randyka.But how to use AsyncTask in your method.Sorry for the trouble. New to Android – Amit Nair Oct 07 '15 at 06:17
  • Thanks Randyka.Tried your method.But Getting error new PostData.execute(Name,Phonenumber,Password,Email,Street,House,Landmark); "Cannot resolve symbol execute". – Amit Nair Oct 07 '15 at 06:36
  • I have called the new PostData on buttonclick – Amit Nair Oct 07 '15 at 06:39
  • I put new PostData.execute(); inside buttonclick listener function and private class PostData extends AsyncTask < String, Void, Void > outside the onCreateView in fragment.Where I have done mistake.Please guide me. – Amit Nair Oct 07 '15 at 06:48
  • @AmitNair you just call it with `new PostData().execute(Name, PhoneNumber..and so on);` – Randyka Yudhistira Oct 07 '15 at 06:49
  • @Randyka..Now also data not getting inserted..only showing succes message in api – Amit Nair Oct 07 '15 at 07:59
  • @AmitNair check your server – Randyka Yudhistira Oct 07 '15 at 08:00
  • thanks @Randyka..now I am getting values in namevaluepair.But String Name = params[0]; method was not working for me..I tried Name = full_name.getText().toString(); ,,where full_name is the id of my edit text field.Thanks – Amit Nair Oct 10 '15 at 04:33
  • HttpClient is not supported by default anymore, I think we have to use httpUrlConnection instead. See a post: https://stackoverflow.com/a/32153434/8539070 – jonasxd360 Jul 16 '19 at 17:54