-1

I have an Android app communicating with an external MySQL Database. I can do my operations without any problem, but each query generates a lot of code, ( HTTPost + Json Parsing) and I have a lot of queries.

Is there a way to factorise code, a good lib, or something like that?

peterh
  • 11,875
  • 18
  • 85
  • 108
Juliatzin
  • 18,455
  • 40
  • 166
  • 325

1 Answers1

1

There is AsyncHttpClient: http://loopj.com/android-async-http/
where you get onSuccess and onFailure methods to work with the response.

To parse your data:
As your response is JSON format, you may be better off using Gson library to map data and with custom model classes. Eg.:

    Gson gson = new Gson();
    ModelClass modelClass= new ModelClass();
    modelClass= gson.fromJson(responseContent,ModelClass.class); 
//where responseContent is your jsonString
    Log.i("Web service response", ""+modelClass.toString());

More on: https://code.google.com/p/google-gson/

For Naming discrepancies(according to the variables in webservice), can use annotations like @SerializedName.

Use a for each loop to verify/browse/access the data that would be populated in/as objects/fields of your model class:
Check these for doubts:
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
How does the Java 'for each' loop work?

Community
  • 1
  • 1
Pararth
  • 8,114
  • 4
  • 34
  • 51