Android: Updating You App – New Version

September 27, 2012

Now that the first version of my application is in the Play Store, i have started thinking on how i can improve on it and what i will need to have in Version 2.0

One of the things that bothered me with version 1 was that the data of my app was stored in the app and that made the app static

I decided to store the app data in the cloud and make my app dynamic

  • Created a Data Model for my application and mapped it to “Class” / “Table” in Parse
  • I stored data in the parse cloud – created CSV files with the data and use the CSV File Import in Parse
  • Updated the application to use the Parse Android API to do queries and retrieve the data from Parse. Check my blog entry on Android and Parse / First Parse Application to get more details. I am including a code snippet here that shows the usage of the Parse API

ParseQuery query = new ParseQuery(“VrChildrenContent”);
query.whereEqualTo(“ContentGroupName”, cartoonName);

try {
contentList = query.find();

} catch (ParseException e) {

}

I am not yet ready to deploy the second version, i may have some more enhancements to do. Once i am ready – I will also describe the process of changing the manifest to specify the new version no: etc.

BTW – Before I started changing code in my app, i created a copy of the app in eclipse and renamed it to VrChildren_v10, so i can go back to the deployed v1.0 at at anytime.

Advertisements

Working with REST and JSON in Android

September 16, 2012
  1. You can invoke back end services from your Android Application and a typical execution model involves invoking the services over HTTP and processing the data – in JSON format returned by them
  2. I registered for the weather update service and use this as the back end service in this application – http://www.myweather2.com/developer/forecast.ashx?uac=1xakeBT0He&output=json&query=10036
  3. To make sure the service is working, you can execute the URL in any browser and you will see the result, you can format the result using this free JSON formatter – http://jsonformat.com/. Copy and paste the response and click on the process button and you will see the results in a more readable fashion.
  4. To process any JSON response, i have used a JSON Parser Class and included is the code for that
  5. public class JSONParser {static InputStream is = null;
    static JSONObject jObj = null;
    static String json = “”;

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
    // defaultHttpClient
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);

    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();
    is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    } catch (ClientProtocolException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }

    try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
    is, “iso-8859-1”), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
    sb.append(line + “n”);
    }
    is.close();
    json = sb.toString();
    Log.i(“JSON Parser”, json.toString());
    } catch (Exception e) {
    Log.e(“Buffer Error”, “Error converting result ” + e.toString());
    }

    // try parse the string to a JSON object
    try {
    jObj = new JSONObject(json);
    } catch (JSONException e) {
    Log.e(“JSON Parser”, “Error parsing data ” + e.toString());
    }

    // return JSON String
    return jObj;

    }
    }

  6. As of Honeycomb (SDK 11), Android prevents invoking Http Services etc. from the main thread, the asynchronous approach is the way to go. A NetworkOnMainThreadException gets thrown when you try to run an HTTP request from the main thread.
  7. Therefore you create a private class in your Activity that extends AsyncTask. The code below shows the Activity class with the private class
  8. import org.json.JSONObject;import android.os.AsyncTask;
    import android.os.Bundle;
    import android.app.Activity;
    import android.util.Log;
    import android.view.Menu;
    import android.widget.TextView;
    public class SecondActivity extends Activity {

    private String passedVarId = null;
    TextView textView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    passedVarId = getIntent().getStringExtra(“ID”);
    textView = (TextView) findViewById(R.id.passedDataTextView);
    WeatherParser task = new WeatherParser();
    task.execute(“http://stackoverflow.com”);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }

    private class WeatherParser extends AsyncTask<String, String, String>{

    private String temp = “”;

    @Override
    protected String doInBackground(String… uri) {
    try {
    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();
    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(“http://www.myweather2.com/developer/forecast.ashx?uac=1xakeBT0He&output=json&query=91320&#8221;);
    Log.i(“WeatherParser:”, json.toString());
    JSONObject currentWeather = (JSONObject)json.getJSONObject(“weather”).getJSONArray(“curren_weather”).get(0);
    temp = (String)currentWeather.get(“temp”);
    Log.i(“WeatherParser:”, temp);
    } catch (Exception e) {
    temp = “Error Getting Weather Update”;
    Log.e(“WeatherParser:”, “Error parsing data:” + e.toString());
    e.printStackTrace();
    }

    return temp;
    }

    @Override
    protected void onPostExecute(String result) {
    super.onPostExecute(result);
    textView.setText(temp);
    }
    }
    }

  9. You will notice that in the private class I have used the JSON Parser to process the generic JSON response and then used message specific parsing of that JSON response to retrieve the details
  10. To update the data received from the Async call to UI fields in the Activity – code that logic in the onPostExecute method of the private class:protected void onPostExecute(String result) {
    super.onPostExecute(result);
    textView.setText(temp);
    }