Transfer data from PHP to Android (JSON) - Part 2

Transfer data from PHP to Android (JSON) - Part 2

This is part 2 of 3 about transferring data from a PHP page to an android app using JSON.

you can read part 1 here.

Creating Client Side (Java):

  1. AndroidManifest.xml:

    <uses-permission android:name="android.permission.INTERNET" />
    
  2. in your activity, service, widget, etc…:

    Decode(downloadFile("http://www.example.com/SQL.php"));
    
    public static void Decode(CharSequence result) {
    // TODO decode the JSON CharSequence (result)
    }
    
  3.  /* Helper function. put it in the same page, or in a library */
     protected static String downloadFile(String url) {
    
     // to fill-in url content
     StringBuilder builder = new StringBuilder();
    
     // local objects declarations
     HttpClient client = new DefaultHttpClient();
     HttpGet httpGet = new HttpGet(url);
    
     try {
     HttpResponse response = client.execute(httpGet);
     StatusLine statusLine = response.getStatusLine();
     int statusCode = statusLine.getStatusCode();
     if (statusCode == 200) {
     HttpEntity entity = response.getEntity();
     InputStream content = entity.getContent();
     BufferedReader reader = new BufferedReader(
     new InputStreamReader(content));
     String line;
     while ((line = reader.readLine()) != null) {
     builder.append(line);
     }
     } else {
     // Failed to download file
    
     }
     } catch (ClientProtocolException e) {
     e.printStackTrace();
     } catch (IOException e) {
     e.printStackTrace();
     } catch (Exception e) {
     e.printStackTrace();
     }
     return builder.toString();
     }
    

 

Decoding is done differently for every JSON string.

in the next article you will learn how to decode your JSON string ->