0
    package demo.com.mysqldbdemo;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;

/**
 * Created by Ahmadzai on 7/10/2017.
 */

public class BackgroundTask extends AsyncTask<String, Void, String> {

    Context ctx;
    BackgroundTask(Context ctx)
    {
        this.ctx = ctx;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        String reg_url = "http://192.168.8.100:8081/webapp/register.php";
        String login_url = "http://localhost/webapp/login.php";
        String method = params[0];
        if (method.equals("register"))
        {
            String name = params[1];
            String user_name = params[2];
            String user_pass = params[3];

            try {
                URL url = new URL(reg_url);
                HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
                httpsURLConnection.setRequestMethod("POST");
                httpsURLConnection.setDoOutput(true);
                OutputStream OS = httpsURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
                String data = URLEncoder.encode("user","UTF-8") + "=" +URLEncoder.encode(name,"UTF-8")+"&"+
                        URLEncoder.encode("user_name","UTF-8") + "=" +URLEncoder.encode(user_name,"UTF-8")+"&"+
                        URLEncoder.encode("user_pass","UTF-8") + "=" +URLEncoder.encode(user_pass,"UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();

                InputStream IS = httpsURLConnection.getInputStream();
                IS.close();
                return "Registration succeed ....";

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
    }
}

I want to insert data into mysql table, as this code does not have any error but when I press the register button in the interface the application stops running when I want to debug the application it runs till "HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();" line of code after this it stops debugging. Anyone can help me in this regard, please? I have been using stackoverflow answers but this is my first try on questioning, sorry for the inconvenience.

5
  • What does your logcat say? Commented Jul 10, 2017 at 13:38
  • I recommend you to read about Retrofit Commented Jul 10, 2017 at 13:41
  • 1
    I'm betting on class cast exception, because your URL does not represent connection via HTTPS. Commented Jul 10, 2017 at 13:46
  • @M.Prokhorov Now that you mention it, that is completely right, I didn't notice that.... Commented Jul 10, 2017 at 13:49
  • thank you dears it has been solved Commented Jul 13, 2017 at 6:48

1 Answer 1

1

start using retrofit it is best for android

Sign up to request clarification or add additional context in comments.

1 Comment

If you recommend a tool, please also describe how that tool can be used to solve the problem at hand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.