I would like to fetch data from a phpMyAdmin server. The only fun开发者_JAVA百科ction 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. 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. 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.
Them make a HTTP POST or HTTP GET request from the android application to the server to get the required data. As for the user name and password. Are you refering to an SSL certificate. 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?
PHPMyAdmin
is a selection of pages that does what you want to do for a browser: extract information from a database. 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.
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).
look for instance here for more information on how to do that: 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.
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....");
P.S : I don't know android coding ,but general solution will be like above :)
精彩评论