android retrieve data from mysql database by using Gson

Json object Response Single Object

Step 1 : – Put a internet permission manifest file

<?xml version=“1.0” encoding=“utf-8”?>

<manifest xmlns:android=“http://schemas.android.com/apk/res/android”

  package=“com.gantasoft.jsonsingleobject”>

<uses-permission android:name=“android.permission.INTERNET”></uses-permission>

  <application

      android:allowBackup=“true”

      android:icon=“@mipmap/ic_launcher”

      android:label=“@string/app_name”

      android:roundIcon=“@mipmap/ic_launcher_round”

      android:supportsRtl=“true”

      android:theme=“@style/AppTheme”>

      <activity android:name=“.MainActivity”>

          <intent-filter>

              <action android:name=“android.intent.action.MAIN” />


              <category android:name=“android.intent.category.LAUNCHER” />

          </intent-filter>

      </activity>

  </application>


</manifest>

Step 2: Put a Gson Library in Build.gradle(Module: app):

dependencies {

  implementation fileTree(dir: ‘libs’, include: [‘*.jar’])

  implementation ‘com.android.support:appcompat-v7:28.0.0’

  implementation ‘com.android.support.constraint:constraint-layout:1.1.3’

  testImplementation ‘junit:junit:4.12’

  implementation ‘com.google.code.gson:gson:2.2.4’

  androidTestImplementation ‘com.android.support.test:runner:1.0.2’

  androidTestImplementation ‘com.android.support.test.espresso:espresso-core:3.0.2’

}

Click Syncnow button it will shows on build.gradle top right side

 

Step 3 : Create a DTO class UserDTO.java

 

package com.gantasoft.jsonsingleobject;


public class UserDTO {

  String user_id,user_name,user_email,user_mobile,user_location;


  public String getUser_id() {

      return user_id;

  }


  public void setUser_id(String user_id) {

      this.user_id = user_id;

  }


  public String getUser_name() {

      return user_name;

  }


  public void setUser_name(String user_name) {

      this.user_name = user_name;

  }


  public String getUser_email() {

      return user_email;

  }


  public void setUser_email(String user_email) {

      this.user_email = user_email;

  }


  public String getUser_mobile() {

      return user_mobile;

  }


  public void setUser_mobile(String user_mobile) {

      this.user_mobile = user_mobile;

  }


  public String getUser_location() {

      return user_location;

  }


  public void setUser_location(String user_location) {

      this.user_location = user_location;

  }



}

 

Step 4 : Create a helper Class(AsynchTask class)  UserHelper.java :

 

package com.gantasoft.jsonsingleobject;


import android.app.ProgressDialog;

import android.os.AsyncTask;

import android.util.Log;


import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;


public class UserHelper extends AsyncTask<String,Void,String> {

  MainActivity mainActivity;

  ProgressDialog progressDialog;

  String url;

  URL surl;


  public UserHelper(MainActivity mainActivity, String url) {

      this.mainActivity = mainActivity;

      this.url = url;

  }


  @Override

  protected void onPreExecute() {

      super.onPreExecute();

      progressDialog=new ProgressDialog(mainActivity);

      progressDialog.setMessage(“waiting…..”);

      progressDialog.show();


  }


  @Override

  protected String doInBackground(String… strings) {

      try {

          surl=new URL(url);

      } catch (MalformedURLException e) {

          e.printStackTrace();

      }


      return GET(surl);

  }

  public static String GET(URL url){

      InputStream inputStream = null;

      String result = “”;

      HttpURLConnection conn = null;

      try {


          Log.e(“URL”, “> ” + url);

          conn = (HttpURLConnection) url.openConnection();

          inputStream = new BufferedInputStream(conn.getInputStream());

          if(inputStream != null)

              result = convertInputStreamToString(inputStream);

          else

              result = “Did not work!”;


      }

      catch (Exception e) {

          Log.d(“InputStream”, e.getLocalizedMessage());

      }

      return result;

  }


  @Override

  protected void onPostExecute(String result) {

      super.onPostExecute(result);

      progressDialog.dismiss();

      if(result!=null&&result.trim().length()>0){

          Log.i(“userhelper”,“The result is : “+result);

      mainActivity.setValues(result);

      }

  }

  private static String convertInputStreamToString(InputStream inputStream) throws IOException {

      BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));

      String line = “”;

      String result = “”;

      while((line = bufferedReader.readLine()) != null)

          result += line;

      inputStream.close();

      return result;

  }

}

Step 5 : Create an Activity Mainactivity.java :

 

package com.gantasoft.jsonsingleobject;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TextView;


import com.google.gson.Gson;


public class MainActivity extends AppCompatActivity {

 TextView textView,textView1,textView2,textView3,textView4;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);


      textView=findViewById(R.id.text);

      textView1=findViewById(R.id.text1);

      textView2=findViewById(R.id.text2);

      textView3=findViewById(R.id.text3);

      textView4=findViewById(R.id.text4);

      UserHelper userHelper=new UserHelper(MainActivity.this,“http://gantasoft.com/Webservices/user_indual.php”);

      userHelper.execute();

  }


  public  String setValues(String result){


      Gson gson=new Gson();

      UserDTO userDTO=gson.fromJson(result,UserDTO.class);

      textView.setText(userDTO.getUser_name());

      textView1.setText(userDTO.getUser_email());

      textView2.setText(userDTO.getUser_id());

      textView3.setText(userDTO.getUser_location());

      textView4.setText(userDTO.getUser_mobile());


   return null;

  }


}

 

Step 6 : Create an Xml File activity_main.xml

 

<?xml version=“1.0” encoding=“utf-8”?>

<android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”

  xmlns:app=“http://schemas.android.com/apk/res-auto”

  xmlns:tools=“http://schemas.android.com/tools”

  android:layout_width=“match_parent”

  android:layout_height=“match_parent”

  tools:context=“.MainActivity”>

<LinearLayout

  android:layout_width=“match_parent”

  android:orientation=“vertical”

  android:layout_height=“match_parent”>

  <TextView

      android:layout_width=“match_parent”

      android:id=“@+id/text”

      android:layout_height=“wrap_content” />

  <TextView

      android:layout_width=“match_parent”

      android:id=“@+id/text1”

      android:layout_height=“wrap_content” />

  <TextView

      android:layout_width=“match_parent”

      android:id=“@+id/text2”

      android:layout_height=“wrap_content” />

  <TextView

      android:layout_width=“match_parent”

      android:id=“@+id/text3”

      android:layout_height=“wrap_content” />

  <TextView

      android:layout_width=“match_parent”

      android:id=“@+id/text4”

      android:layout_height=“wrap_content” />


</LinearLayout>

</android.support.constraint.ConstraintLayout>



Downlaod Apk File : http://gantasoft.com/Webservices/app-debug.apk