[Solved] Android app . fetching data from database [closed]


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

2

solved Android app . fetching data from database [closed]