I am new to android development, I developed an application on android which has 3 activities, beside this I have developed a PHP website having database on MySQL. I want to make connection between android and PHP. I read alot of tutorials and make connections according to that using JSON Parser but I doesn't work. Can It is possible to connect client to server using "Web Services"? Now the requirement of my project is that I have a form of Customer Registration from which I want to get all the data that is entered by user and that is saved in database on android. How can I do it? Is it possible that in my android application I enter data in my other activities and it will save in MySql database. Kindly help because I have tried many ways to connect and getting data but didn't succeed Kindly guide the possible way to retrieve Task details from my PHP application.
Asked
Modified
12 years, 11 months ago
Viewed
2k times
Part
of PHP and Mobile Development Collectives
2
-
1Yes, it's possible to use webservices. in fact, it's about the only practical way to do it. And no, we don't do it for you.Marc B– Marc B2012-12-30 15:37:18 +00:00Commented Dec 30, 2012 at 15:37
-
I just want suggestion that how can I do it? you can refer me a example or tutorial. It'll be helpful for me. I just need guide not the exact solution. thanks for your responseAndroid– Android2012-12-30 15:40:15 +00:00Commented Dec 30, 2012 at 15:40
Add a comment
|
2 Answers
you can make a RESTFUL API service for your web application which parse json, xml, csv and many other types, then you can call it form android.
one of the ways to call the service from android is android query
1 Comment
Android
Ok i try, thanks for such helpful guide:)
Do like this.
It might help to you.
title = spinnerTitle.getSelectedItem().toString();
firstName = editTextFirstName.getText().toString();
lastName = editTextLastName.getText().toString();
address1 = editTextAddress1.getText().toString();
adddress2 = editTextAddress2.getText().toString();
city = editTextCity.getText().toString();
county = editTextCounty.getText().toString();
postCode = editTextPostCode.getText().toString();
country = spinnerCountry.getSelectedItem().toString();
dayPhone = editTextDayPhone.getText().toString();
evePhone = editTextEvePhone.getText().toString();
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
+ "<users><Title>"+title+"</Title>"
+ "<FirstName>"+firstName+"</FirstName>"
+ "<LastName>"+lastName+"</LastName>"
+ "<Address1>"+address1+"</Address1>"
+ "<Address2>"+adddress2+"</Address2>"
+ "<LoginID>"+userId+"</LoginID>"
+ "<Password>"+password+"</Password>"
+ "<County>"+county+"</County>"
+ "<City>"+city+"</City>"
+ "<Country>"+country+"</Country>"
+ "<PostCode>"+postCode+"</PostCode>"
+ "<Email>"+email+"</Email>"
+ "<DayPhone>"+dayPhone+"</DayPhone>"
+ "<EvePhone>"+evePhone+"</EvePhone>" + "</users>";
serverCall(xml);
private void serverCall(final String xml )
{
ConnectivityManager conMan = ( ConnectivityManager ) getSystemService( Context.CONNECTIVITY_SERVICE );
if( conMan.getActiveNetworkInfo() != null && conMan.getActiveNetworkInfo().isConnected() )
{
result = new Connection().getResponse("http://localhost/registration.php" , xml);
if(result.equals("yes") {
//registration done
} else {
//failed
}
}
}
Connection.java
public String getResponse(String connUrl, String xml) {
DefaultHttpClient httpClient = null;
try {
MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
HttpPost method = new HttpPost(connUrl);
method.setEntity(mp);
mp.addPart("xml_request", new StringBody(xml));
httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(method);
if(response.getStatusLine().getStatusCode() == 200){
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
response.getEntity().writeTo(outstream);
return outstream.toString();
}
}
catch(Exception e) {
Log.v("APP", "Exception while create connection for getResponse from server with: " + e.getMessage());
}
finally {
httpClient.getConnectionManager().shutdown();
}
return "";
}