I am a new Android app developer and need some help. I want to develop a simple login app just for understanding the working. Using sqlite we can create tables and insert records in our application , but how to keep the table centralised for username and password so that all the users can access the same table from the app for login instead of creating the database on your device. Please help with some tips or any links which will help me clearify my doubts. Thanks in advance.
-
2You need a server that stores the user name and password probably in encrypted format in database – Raghunandan Oct 18 '16 at 05:16
-
1See https://firebase.google.com/ – Diego Torres Milano Oct 18 '16 at 05:18
3 Answers
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")
public class DoubleMeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public DoubleMeServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getOutputStream().println("Hurray !! This Servlet Works");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
int length = request.getContentLength();
byte[] input = new byte[length];
ServletInputStream sin = request.getInputStream();
int c, count = 0 ;
while ((c = sin.read(input, count, input.length-count)) != -1) {
count +=c;
}
sin.close();
String recievedString = new String(input);
response.setStatus(HttpServletResponse.SC_OK);
OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());
Integer doubledValue = 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;
public class DoubleMeActivity extends Activity implements OnClickListener {
EditText inputValue=null;
Integer doubledValue =0;
Button doubleMe;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculate);
inputValue = (EditText) findViewById(R.id.inputNum);
doubleMe = (Button) findViewById(R.id.doubleme);
doubleMe.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.doubleme:
new Thread(new Runnable() {
public void run() {
try{
URL url = new URL("http://10.0.2.2:8080/MyServletProject/DoubleMeServlet");
URLConnection connection = url.openConnection();
String inputString = inputValue.getText().toString();
//inputString = URLEncoder.encode(inputString, "UTF-8");
Log.d("inputString", inputString);
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(inputString);
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String returnString="";
doubledValue =0;
while ((returnString = in.readLine()) != null)
{
doubledValue= Integer.parseInt(returnString);
}
in.close();
runOnUiThread(new Runnable() {
public void run() {
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

- 971
- 8
- 23
-
Thanks...can you tell me can we use sny other language other than php like servlets ? – Aditya Jadhav Oct 20 '16 at 18:45
-
1I have edited my answer with the servlet connectivity to android app and better to change your question to 'Android app . fetching data from database using servlet' - it will easiest way to answer in a single time – Bethan Oct 21 '16 at 07:27
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.

- 71
- 1
- 9
-
Yes i know about frontend and backend, also that sqlite will store data only for ur application. I want to ask that instead of php can we use any other server side script like servlet or something. . – Aditya Jadhav Oct 20 '16 at 18:43
-
yes you can use any! you may use node(i had used it), use servlet or whtever, backend and frontend are loosely coupled things. you may use anything for backend its completely your choice. – Anubhav Sahu Oct 21 '16 at 10:35
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.

- 1,685
- 2
- 17
- 28