1

I am a newbie attempting to send data from an Android app to a MySQL database set up on a localhost server via xampp. This method is supposed to put a name, username, password, phone number, and age into an arraylist of type NameValuePair.

@Override
    protected Void doInBackground(Void... params) {
            ArrayList<NameValuePair> dataToSend = new ArrayList<>();
            dataToSend.add(new BasicNameValuePair("name", user.name));
            dataToSend.add(new BasicNameValuePair("age", user.age + ""));
            dataToSend.add(new BasicNameValuePair("username", user.username));
            dataToSend.add(new BasicNameValuePair("password", user.password));
            dataToSend.add(new BasicNameValuePair("phoneNumber", user.phoneNumber));

        HttpParams httpRequestParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);

        HttpClient client = new DefaultHttpClient(httpRequestParams);

        HttpPost post = new HttpPost("localhost/Register.php");

        try{
            post.setEntity(new UrlEncodedFormEntity(dataToSend));
            client.execute(post);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

When I run the application, I receive the following exception:

Target host must not be null, or set in parameters. scheme=null, host=null, path=localhost/Register.php

My research leads me to believe that there's a problem with the url string, but I don't know specifically what the issue is.

Thanks in advance.

(P.S. I am also aware of the awful security implications of storing a password in plain text on a database. Just trying to learn how to interact between apps and databases. Thx)

6
  • 1
    Try to set 10.0.2.2 this IP instead of localhost Commented Jun 11, 2015 at 3:23
  • @SarvagnaMehta what exactly does this do? Commented Jun 11, 2015 at 3:43
  • It connects you to localhost, because in some emulators localhost is directly not working. Commented Jun 11, 2015 at 4:26
  • use url like this "http://SYSTEM_IP_ADDRESS/Register.php" Commented Jun 11, 2015 at 5:49
  • @bharat is SYSTEM_IP_ADDRESS the same as the localhost address? Commented Jun 11, 2015 at 18:31

2 Answers 2

2

Try this code to post data if the localhost replacement doesn't work for you.

   HttpParams params = new DefaultHttpParams(); // setup whatever params you what
    HttpClient client = new DefaultHttpClient(params);
    HttpPost post = new HttpPost("someurl");
    post.setEntity(new UrlEncodedFormEntity()); // with list of key-value pairs
    client.execute(post, new ResponseHandler(){}); // implement ResponseHandler to handle response correctly.
Sign up to request clarification or add additional context in comments.

Comments

0

My issue was simply that I was using the wrong ip address. For some reason or another, localhost does not work. I figured out my system's ipv4 address (not 10.0.2.2) and used that and it worked.

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.