1

I have problem. I can not connect to MySQL server. I followed this tutorial simplifiedcoding.net/android-php-mysql-login-tutorial-android-login-app-3/ and I wanted to make simple PHP MySQL login. But some things like 'NameValuePair', 'BasicNameValuePair', 'HttpClient', 'HttpPost', 'HttpResponse', 'HttpEntity', 'UrlEncodedFormEntity' and much more are deprecated. I wanted to ask, could anyone code this right? Or just help me?

Here is the code which is not working correctly: (and thanks for your help)

@Override
            protected String doInBackground(String... params) {
                String uname = params[0];
                String pass = params[1];

                InputStream is = null;
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("username", uname));
                nameValuePairs.add(new BasicNameValuePair("password", pass));
                String result = null;

                try{
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost("site.com/login.php");
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse response = httpClient.execute(httpPost);

                    HttpEntity entity = response.getEntity();

                    is = entity.getContent();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return result;
            }
4
  • Apache HttpClient is deprecrated and has to be replaced by HttpUrlConnection. See here: developer.android.com/reference/java/net/HttpURLConnection.html Commented Jul 12, 2015 at 10:22
  • And your question title is not reflecting your real question. Please change it. You are asking about http connection handling in android app not about jdbc connection problems in AS. Commented Jul 12, 2015 at 10:23
  • could anyone help how to code this right? With good explain? Commented Jul 12, 2015 at 13:57
  • stackoverflow.com/questions/8654876/… Commented Jul 12, 2015 at 14:34

1 Answer 1

0

Actually the link you posted is from my website. And when I written this tutorial the classes were not deprecated. If you will check my website then I have already posted an updated tutorial.

To send a http post request to your web server you can use the following code.

public class RequestHandler {
public String sendPostRequest(String requestURL,
                              HashMap<String, String> postDataParams) {
    URL url;

    StringBuilder sb = new StringBuilder();
    try {
        url = new URL(requestURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        OutputStream os = conn.getOutputStream();

        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));

        writer.flush();
        writer.close();
        os.close();
        int responseCode = conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {

            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            sb = new StringBuilder();
            String response;
            while ((response = br.readLine()) != null){
                sb.append(response);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for (Map.Entry<String, String> entry : params.entrySet()) {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();
}
}

Now all you need to do is inside you main activity create an AsyncTask and inside doInBackGround method

call the sendPostRequest() method

you have to pass the url of you script to handle the post request and the parameters in a hashmap to send data along with the request.

For more details visit Android PHP MySQL - CRUD Operation

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

Comments

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.