Reputation: 105
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)
Upvotes: 1
Views: 1370
Reputation: 105
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.
Upvotes: 0
Reputation: 340
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.
Upvotes: 2