Android App . Fetching Data From Database
Solution 1:
If you wanna use the database as centralized then it should be a server but not Local DB. Local db can be accessible within the device as we all know and the below can help you to handle the server.
Step 1: First buy a server in either GoDaddy or 000webhost.com(Free server will be available)
step 2: Create a database and make some tables which matches your requirement.
step 3: Remaining all coding and integrating part is in this Url Android Php connect
This is the basic API for the starters, keep going.
Android API integration using Java servlet
Step 1: Download Eclipse EE(Express Edition) or Add Eclipse EE plugin to your Existing Eclipse
Step 2: In your Eclipse go to File > New > Project > Web > Dynamic Web Project > next.
Step 3: Name your Project and select Apache Tomcat v7.0 as the target runtime and click finish.
Step 4: Now right click on project > New > Other > Web > Servelt. Name the Servlet as your wish.
Step 5: As an Example I placed here two multiply the number with 2
import java.io.IOException;
import java.io.OutputStreamWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/DoubleMeServlet")publicclassDoubleMeServletextendsHttpServlet {
privatestaticfinallongserialVersionUID=1L;
publicDoubleMeServlet() {
super();
}
protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.getOutputStream().println("Hurray !! This Servlet Works");
}
protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
try {
intlength= request.getContentLength();
byte[] input = newbyte[length];
ServletInputStreamsin= request.getInputStream();
int c, count = 0 ;
while ((c = sin.read(input, count, input.length-count)) != -1) {
count +=c;
}
sin.close();
StringrecievedString=newString(input);
response.setStatus(HttpServletResponse.SC_OK);
OutputStreamWriterwriter=newOutputStreamWriter(response.getOutputStream());
IntegerdoubledValue= Integer.parseInt(recievedString) * 2;
writer.write(doubledValue.toString());
writer.flush();
writer.close();
} catch (IOException e) {
try{
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getWriter().print(e.getMessage());
response.getWriter().close();
} catch (IOException ioe) {
}
}
}
}
Step 6: Right click on Servlet project > Run as > Run on Server. Run on Server Dialog should pop up. Select "Manually define a new server" and also Tomcat v7.0 under server type." Before that make sure your tomcat server is up and running. Open your web browser and type http://localhost:8080. You should see a default page displayed by tomcat server.
And below is the sample code, you should call your servlet file from your android app like this mentioned way
package com.app.myapp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
publicclassDoubleMeActivityextendsActivityimplementsOnClickListener {
EditText inputValue=null;
IntegerdoubledValue=0;
Button doubleMe;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculate);
inputValue = (EditText) findViewById(R.id.inputNum);
doubleMe = (Button) findViewById(R.id.doubleme);
doubleMe.setOnClickListener(this);
}
@OverridepublicvoidonClick(View v) {
switch (v.getId()){
case R.id.doubleme:
newThread(newRunnable() {
publicvoidrun() {
try{
URLurl=newURL("http://10.0.2.2:8080/MyServletProject/DoubleMeServlet");
URLConnectionconnection= url.openConnection();
StringinputString= inputValue.getText().toString();
//inputString = URLEncoder.encode(inputString, "UTF-8");
Log.d("inputString", inputString);
connection.setDoOutput(true);
OutputStreamWriterout=newOutputStreamWriter(connection.getOutputStream());
out.write(inputString);
out.close();
BufferedReaderin=newBufferedReader(newInputStreamReader(connection.getInputStream()));
String returnString="";
doubledValue =0;
while ((returnString = in.readLine()) != null)
{
doubledValue= Integer.parseInt(returnString);
}
in.close();
runOnUiThread(newRunnable() {
publicvoidrun() {
inputValue.setText(doubledValue.toString());
}
});
}catch(Exception e)
{
Log.d("Exception",e.toString());
}
}
}).start();
break;
}
}
}
Refer the below link for complete reference Connect android app with servlet
Solution 2:
Are you aware of the backend and frontend? sqlitedatabase doesnot stores the universal data which is available for all the users. the database you are talking about needs to be stored in the server side or what people also say backend. that is the "centralised part" you are talking about. read about frontend and backend, and then about http requests.
Solution 3:
Here the things you can do: 1. Without a backend server, you can't do it in centralized way. 2. Use firebase. This provides everything what you need. This provides a NoSQL structured database. You can refer to docs for more information. 3. You can use A SQL server with PHP as a scripting support. Try WebHost to start for free. Read this pdf chapter no 55(page no 327) for step by step settings.
Post a Comment for "Android App . Fetching Data From Database"