简体   繁体   English

Android服务器数据获取

[英]Android server data fetching

I would like to fetch data from a phpMyAdmin server. 我想从phpMyAdmin服务器获取数据。 The only function I know, is this : 我知道的唯一功能是:

private String getPage() {
            String str = "***";

            try
            {
                HttpClient hc = new DefaultHttpClient();
                HttpPost post = new HttpPost("http://mywebsite.me");
                HttpResponse rp = hc.execute(post);

                if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
                {
                    str = EntityUtils.toString(rp.getEntity());
                }
            }catch(IOException e){
                e.printStackTrace();
            }  

            return str;
        }

It doesn't work because I need to authenticate myself with an username and password. 它不起作用,因为我需要使用用户名和密码进行身份验证。 Another reason that I think this function will not work is that it grabs the source of the page ( ctrl + u in Chrome) and the values that I need aren't there, they are in a table on the server. 我认为此功能无法正常工作的另一个原因是,它捕获了页面的源代码(Chrome中的ctrl + u ),并且我所需的值不存在,它们位于服务器上的表中。 How could I do that? 我该怎么办?

I don't know if have gotten your answer but the straightforward way is to create a php file with a query to extract the data you need in the database and output the data in xml or json format from the php file. 我不知道是否能得到您的答案,但是直接的方法是使用查询创建一个php文件,以提取数据库中所需的数据,并从php文件中以xml或json格式输出数据。 If you need an example check: 如果需要示例检查:

http://webdesignergeeks.com/mobile/android/android-login-authentication-with-remote-db/ it shows you how to get data from database in php and how to make a http request from the android app. http://webdesignergeeks.com/mobile/android/android-login-authentication-with-remote-db/向您展示了如何从php数据库中获取数据以及如何从android应用中发出http请求。

Them make a HTTP POST or HTTP GET request from the android application to the server to get the required data. 他们从android应用向服务器发出HTTP POST或HTTP GET请求,以获取所需的数据。 As for the user name and password. 至于用户名和密码。 Are you refering to an SSL certificate. 您是指SSL证书吗? Where exactly do you need the username and password? 您究竟在哪里需要用户名和密码?

You're going about it the wrong way I think. 您正在以错误的方式思考。 I presume that phpAdmin is actually phpmyadmin, and you are looking for data from your database? 我认为phpAdmin实际上是phpmyadmin,并且您正在从数据库中查找数据?

PHPMyAdmin is a selection of pages that does what you want to do for a browser: extract information from a database. PHPMyAdmin是执行浏览器所需的页面的一部分:从数据库中提取信息。 You don't need to go trough that page to get the info. 您无需浏览该页面即可获取信息。 You can either write some .php files that extract the data for you (for instance in XML or JSON) and fetch them with above code, or try to contact you database directly, without intervention of the apache server. 您可以编写一些.php文件来为您提取数据(例如,以XML或JSON格式),并使用上述代码获取它们,或者尝试直接与您的数据库联系,而无需apache服务器的干预。

Edit: it looks like contacting directly isn't too easy, and you'r login should be valid for external connects (which it might not be). 编辑:看起来直接联系不太容易,您的登录名对于外部连接应该是有效的(可能不是)。 I guess you should write a so-called "REST" interface (a bunch of files that does basically what phpmyadmin does, but not as complicated: you request a table and it gives you this without all the other stuff phpmyadmin shows). 我猜您应该编写一个所谓的“ REST”接口(一堆文件基本上可以完成phpmyadmin的工作,但并不那么复杂:您请求一个表,并且它为您提供了phpmyadmin显示的所有其他内容)。

look for instance here for more information on how to do that: http://www.helloandroid.com/tutorials/connecting-mysql-database 在此处查找示例,以获取有关如何执行此操作的更多信息: http : //www.helloandroid.com/tutorials/connecting-mysql-database

If your website need authentication then use livehttpheader addon (firefox) or wireshark to capture out going POST request,Then use that url to get the source ( i assume if u successfully logged in , the page will display data from phpmyadmin (the one u talking about) ), then search that source to find values you need. 如果您的网站需要身份验证,则使用livehttpheader插件(firefox)或wireshark捕获正在进行的POST请求,然后使用该URL获取源(我假设如果您成功登录,则该页面将显示phpmyadmin中的数据(关于)),然后搜索该源以查找所需的值。

Example of link should be like this 链接的例子应该像这样

http://mywebsite.me?login=true&username=myusername&password=mypassword&extrafield=xxx....

you have to use this link in your code 您必须在代码中使用此链接

 HttpPost post = new HttpPost("http://mywebsite.me?login=true&username=myusername&password=mypassword&extrafield=xxx....");

PS : I don't know android coding ,but general solution will be like above :) PS:我不知道android编码,但一般的解决方案将是上面的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM