Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to connect to mysql on android studio with php and php conect right but is the android code that is the problem.

What I have tried:

Java
public class BackgroundWorker extends AsyncTask<String, Void, String> {
    Context context;
    ProgressDialog alertDialog;

    public BackgroundWorker(Context context){
        this.context = context;
    }
    @Override
    protected String doInBackground(String... params) {
        String type = params[0];
        String login_url = "http://127.0.0.0/login.php";
        if(type.equals("login")){
            try {
                String user_name = params[1];
                String user_pass = params[2];

                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                httpURLConnection.setUseCaches(false);
                OutputStream outputStream = httpURLConnection.getOutputStream(); //When it get to this line of code it jumps out to the exception 
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("user_name", "UTF-8")+"="+URLEncoder.encode(user_name, "UTF-8")+"&"
                        +URLEncoder.encode("user_pass", "UTF-8")+"="+URLEncoder.encode(user_name, "UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));

                String result = "";
                String line = "";

                while ((line = bufferedReader.readLine()) != null) {
                    result += line;
                }

                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();

                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) { //It jups to this exception and give an error
                e.printStackTrace();
            }
        }
        return null;
    }
Posted
Updated 14-Apr-20 8:25am
v2
Comments
Richard MacCutchan 26-Jul-18 9:27am    
Can you connect successfully from a browser? Also, you could try the PC's real IP address.
David Crow 26-Jul-18 10:01am    
} catch (IOException e) { //It jups to this exception and give an error


And that error would be what exactly?

In such cases provide the full exception message.

But your problem is very probably sourced by specifying an invalid URL:
Java
String login_url = "http://127.0.0.0/login.php";
It must be 127.0.0.1.
 
Share this answer
 
Comments
Member 13147658 26-Jul-18 8:42am    
i changed it, it still has the same error
Jochen Arndt 26-Jul-18 8:49am    
Which error (full message text)?
You encode 2 user_name, instead of user_pass:

URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8");

should be :

URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900