如何从mysql php到android获取数据

How to get data from mysql php to android

本文关键字:android 获取 数据 php mysql      更新时间:2023-09-26

我是android开发的新手,我在android上开发了一个有3个活动的应用程序,除此之外,我还开发了一个PHP网站,在MySQL上有数据库。我想在android和PHP之间建立连接。我读了很多教程,并根据使用JSON解析器进行连接,但我不工作。是否可以使用"Web服务"连接客户端到服务器?现在我的项目的要求是,我有一个客户注册的形式,我想得到所有的数据是由用户输入,这是保存在数据库上的android。我该怎么做呢?是否有可能在我的android应用程序中,我在我的其他活动中输入数据,它将保存在MySql数据库中。请帮助,因为我已经尝试了很多方法来连接和获取数据,但没有成功。请指导从我的PHP应用程序检索任务详细信息的可能方法。

你可以为你的web应用程序创建一个RESTFUL API服务,解析json, xml, csv和许多其他类型,然后你可以从android调用它。

从android调用服务的方法之一是android query

这样做

它可能对你有帮助。

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