Reading web services from Android with JSON

{title}

{title}


We will connect to a server, we will read and pair with JSON to return information to us and finally we will save it in a database on our device.
For those who do not have a server where to perform the tests we can use our device and a Wi-Fi router to perform tests without problem.
The first thing we will do is open any program to edit the Eclipse, Betbeans or whatever we like. In a new project we will create a new class called JSONManager which will have the following static and public method.
 public static JSONObject getJSONfromURL (String url) {InputStream is = null; String result = ""; JSONObject json = null; try {HttpClient httpclient = new DefaultHttpClient (); HttpPost httppost = new HttpPost (url); HttpResponse response = httpclient.execute (httppost); HttpEntity entity = response.getEntity (); is = entity.getContent (); } catch (Exception e) {} 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 (); result = sb.toString (); } catch (Exception e) {} try {json = new JSONObject (result); } catch (JSONException e) {} return json; } 

The method will connect to the URL that we indicate and try to read and parse the response in a JSONObject object.
The first part of the method opens the http connection and gets the response in an InputStream. The second part reads the answer and parses it with a BufferedReader object so that it can finally be passed to a String.
This String is necessary in the third part to create a new JSONObject object that will be what we will finally return.
For the next part we create a new class, which I will call JSONParserLibros. This class will be in charge of reading the help service of the previous method and saving in database.
Let's see how it works, for example in a database of movies.
 private Activity activity; private JSONObject jObject; private ProgressDialog progressDialog = null; private Runnable runReadAndParseJSON; public JSONParserPeliculas (Activity a) {activity = a; } public void readAndParseJSONMovies () throws JSONException {runReadAndParseJSON = new Runnable () {@Override public void run () {try {[/ size] [/ font] readJSONMovies (); } catch (Exception e) {} [/ size] [/ font]}}; Thread thread = new Thread (null, runReadAndParseJSON, "bgReadJSONPeliculas"); thread.start (); progressDialog = ProgressDialog.show (activity, "Downloading information", "Please wait", true); private void readJSON Movies () throws JSONException {jObject = JSONManager.getJSONfromURL ("http://208.87.120.105/android/service.peliculas.php"); if (jObject! = null) parseJSONPeliculas (jObject.getJSONArray ("movies")); activity.runOnUiThread (returnRes); } private void parseJSONMovies (JSONArray booksArray) throws JSONException {for (int i = 0; i <moviesArray.length (); i ++) {Movie l = new Movies (); l.setIdPelicula (movieArray.getJSONObject (i) .getInt ("id")); l.setTitulo (movieArray.getJSONObject (i) .getString ("movie")); l.save (activity); }} private Runnable returnRes = new Runnable () {@Override public void run () {progressDialog.dismiss (); }}; 

Next we have the public method that we will use to call our service readAndParseJsonLibros. This method creates a new thread of execution within which the call to the web service is going to be made, in addition a dialog window that will notify the user that a download is being made.
As we can see, I have put an IP of a local network to make the call to the service, this corresponds to the team where I developed the web service. At the time of testing this, if you have a Wi-Fi router, we must activate the Wi-Fi connection on our Android device and connect to our local network so that we can access the service. In case of not having Wi-Fi we have other options such as mounting the web service on a server on the internet, the data will be downloaded using our 3G connection, or we can also test it with the Android emulator.

  • 0