How to get data from mysql php to android

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.


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 "";
}

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

链接地址: http://www.djcxy.com/p/46194.html

上一篇: 如何构建REST API?

下一篇: 如何从MySQL的PHP​​到Android的数据