How to connect to a Remote Database with Webservices?
I have an 开发者_如何学Pythonandroid APP. I need to connect to a remote D.B. on the internet. All i know is that i have to use a Web Service, installed on the remote DB server.
Whith my Android APP i have to connect to the web service, and the web service will do the logical operations to get the necesary data from the D.B. and will send them to me.
OK, the web service is not a problem, i have a friend that will do it (i have no idea about web services), but i dont know how i have to do the connexion with the Web Services, and also i dont know how have to be the functions of the Web Service.
i need to do select's that give me multiple rows of information, and selects, then i have to send data to the Webservice (the parameters of the "select and the insert"), i supose it's simply to call a function of the webservice with normal parameters? or it's more hard than this?. I have searched for tutorials that show me to do that on google but i can't find a good tutorial that show how to do that...
can someone give me a little help? for example, a good tutorial for beginners connecting android to a remote DB with webservices?
thanks
You can connect to the web using the below code and get the data as a string,probably data send to you will be in the form of JSON or XML which you can parse.
Regarding how to connect to the webservice just give the url that your are connecting to and pass parameters.
String urlstr = "www.yoursite.com/api.php?parameter1="+parameter1+"¶meter2="+parameter2;
URL updateURL = new URL(urlstr);
URLConnection conn = updateURL.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(100);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
String html = new String(baf.toByteArray());
this link might give a clear idea as to how u can use internet data in your application.
This tutorial may help you. I know this works because i used this for an application that i am working on: Try this There are a few errors in the code. I will list the corrections you need to make.
If you read the comments below you can see where people have found problems with the code, but one i noticed is that there are too many "}" at the end. If you take out one of those "}" it should work fine. I believe the "}" at line 43 can be taken out.
Also, there is another error that you can fix: Look at line 13, take out "InputStream" in front of "is = entity.getContent();" Now go above the first try{} block and place this declaration: InputStream is;
That should correct all the problems.
Let me know how that works out for you. If you have any questions, let me know.
Kindly go through https://www.javatpoint.com/android-web-service
One small change
HttpPost httpPost=new HttpPost(
"http://10.0.0.8:7777/HttpPostServlet/servlet/httpPostServlet");
Here 10.0.0.8:7777 cannot be correct
It is the webserver address in which servlet and jsp present.
Hope that helps!!!
This should be the base start and one can do lot of wonders after mastering.
精彩评论