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:
      1
      
      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.INTERNET" />
    2. in your activity, service, widget, etc…:
      1
      2
      3
      4
      5
      
      Decode(downloadFile("http://www.example.com/SQL.php"));
       
      public static void Decode(CharSequence result) {
      // TODO decode the JSON CharSequence (result)
      }
      Decode(downloadFile("http://www.example.com/SQL.php"));
      
      public static void Decode(CharSequence result) {
      // TODO decode the JSON CharSequence (result)
      }
    3. 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      
      /* 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();
      }
      /* 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 ->

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.