0

I am working on a project which will have some part developed as a Android APP and some part as a website. The website would be a content management system, and the android app will be one of the consumer of the data.

Just to be aware, my background is completely .NET, C#. And i am learning, Java and Android, so far good.

I was thinking about the technology i should use, so that the two parts can communicate and also how i can reuse most of the common components like Logging, Instrumentation etc.

Few questions where i am not sure which would be the best choice:

  1. Sharing cross platform code? In android i am writing code in Java, and in the website C#. I am facing an issue where how to re-use the common components like logging. I have heard about some tools like IKVM, but are they really worth? Or writing the code at two places will be a better choice in long term ?

  2. Database: I am planning to go with ASP.NET MVC and MySQL and then on Android i can have a SQLite local DB, which i can sync using a web service. I really want to use MSSQL Server (because i know it very well). Is there a way to sync the MS SQL Server hosted remotely and the local Android SQLite DB? Or any other suggestion.

Also, if anyone has any suggestion in how to architect a hybrid solution like this, it would be really helpful.

Note: I cannot use tools like Xamrin as they are not free, and cost is a worry for now. Also, i want to really develop a native android app, which means i don't want to go with PhoneGap etc.

Thanks in advance.

Jash
  • 942
  • 4
  • 17
  • 40

1 Answers1

2

the best way for android and website to communicate is a web service I have worked on android apps that communicate with a .net web service to access sql server database (insert, update, delete, retrieve data....etc) but I fount that having a local data base causes a big issue in sync. so all my apps dont have local DB and request all info from a local server connected using Wi-Fi.

code wise: you need to use Android AsyncTask to send http (GET,POST) requests to the Web service, this is an example of the code:

public class httpGetProduct extends AsyncTask<Void,Void,String> {
        String result, rs;
        JSONArray jArray;
        String itemcode="",price,desc_ar,desc_en;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            isOnline=ping(); // a function that checks if local server is available

        }

        @Override
        protected String doInBackground(Void... params) {
            if(isOnline)
            try {
                String link = "http://"+ip+":"+port+"/PriceCheckerWS.asmx/get_product_data?barcode="+barcode;
                URL url = new URL(link);
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet();
                request.setURI(new URI(link));
        //execute http get request
                HttpResponse response = client.execute(request);
        // read the reply 
                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                result = sb.toString();
        // split string as the result is inside xml tags
                String[] separated = result.split(">");
                JSONObject jObject = new JSONObject(separated[2]);
                jArray = jObject.getJSONArray("product");
        // read the string in from of JSON array
                JSONObject json_data = jArray.getJSONObject(0);
                itemcode=json_data.getString("Barcode");
                price=json_data.getString("Price");
                desc_ar=json_data.getString("Item_Desc_Ar");
                desc_en=json_data.getString("Item_Desc_En");

                rs = "sucessful";
            } catch (Exception e) {
                rs = "Fail";
            }

            return rs;
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            //after execution is done you can do something here
            if(!isOnline)
                Toast.makeText(getApplicationContext(),
                        "Fail to connect to server", Toast.LENGTH_SHORT).show();
            else if(itemcode.isEmpty())
            {
                if(lang){
                    Toast toast = Toast.makeText(getApplicationContext(),
                            "No information about this item in database", Toast.LENGTH_SHORT);
                    toast.show();
                }
                else{
                    Toast toast = Toast.makeText(getApplicationContext(),
                            R.string.no_data_ar, Toast.LENGTH_SHORT);
                    toast.show();
                }

            }else{
                intent = new Intent(MainActivity.this, ViewProduct.class);
                intent.putExtra("barcode", barcode);
                intent.putExtra("price",price);
                intent.putExtra("desc_ar",desc_ar);
                intent.putExtra("desc_en",desc_en);
            startActivity(intent);}


        }
    }

and in the onCreate you call the asynctask like this:

new httpGetProduct().execute();

hope this helps

Nouran S. Ahmad
  • 503
  • 1
  • 5
  • 19
  • Thanks for the reply. So, how your App works when there is no interenet connection ? In my case, i want to store some data in localDB show that user can view that even with no internet connection. And then i can sync the local DB periodically, or on every web request. Aside, thanks for the code. But i am more interested in knowing how to set up the stack – Jash Oct 06 '15 at 13:17
  • well all the apps I worked on used local connection (no internet) just LAN so the connection is almost always up. and without wifi the app gives a msg to the user "please connect to server" – Nouran S. Ahmad Oct 06 '15 at 13:20