├── Android Code ├── .gitignore ├── app │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── hellohasan │ │ │ └── networkcallwithretrofit │ │ │ └── ExampleInstrumentedTest.java │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── hellohasan │ │ │ │ └── networkcallwithretrofit │ │ │ │ ├── Activity │ │ │ │ └── MainActivity.java │ │ │ │ ├── Interface │ │ │ │ └── ApiInterface.java │ │ │ │ ├── Model │ │ │ │ ├── ServerResponse.java │ │ │ │ └── User.java │ │ │ │ └── Retrofit │ │ │ │ └── RetrofitApiClient.java │ │ └── res │ │ │ ├── layout │ │ │ └── activity_main.xml │ │ │ ├── mipmap-hdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ └── ic_launcher.png │ │ │ ├── values-w820dp │ │ │ └── dimens.xml │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── dimens.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── hellohasan │ │ └── networkcallwithretrofit │ │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle ├── PHP Code └── server_side_code.php ├── README.md └── Retrofit-Simple-Get-Request ├── .gitignore ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── hellohasan │ │ └── retrofitsimplegetrequest │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── hellohasan │ │ │ └── retrofitsimplegetrequest │ │ │ ├── MainActivity.java │ │ │ ├── ServerResponse.java │ │ │ └── network │ │ │ ├── ApiInterface.java │ │ │ └── RetrofitApiClient.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── hellohasan │ └── retrofitsimplegetrequest │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /Android Code/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /Android Code/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /Android Code/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "com.hellohasan.networkcallwithretrofit" 7 | minSdkVersion 14 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', { 24 | exclude group: 'com.android.support', module: 'support-annotations' 25 | }) 26 | implementation 'androidx.appcompat:appcompat:1.1.0' 27 | testImplementation 'junit:junit:4.12' 28 | 29 | // for Retrofit and GSON library 30 | implementation 'com.squareup.retrofit2:retrofit:2.5.0' 31 | implementation 'com.squareup.retrofit2:converter-gson:2.5.0' 32 | } 33 | -------------------------------------------------------------------------------- /Android Code/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/hasan/Android/Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /Android Code/app/src/androidTest/java/com/hellohasan/networkcallwithretrofit/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.hellohasan.networkcallwithretrofit; 2 | 3 | import android.content.Context; 4 | import androidx.test.platform.app.InstrumentationRegistry; 5 | import androidx.test.ext.junit.runners.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.hellohasan.loginandregistrationwithretrofit", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Android Code/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /Android Code/app/src/main/java/com/hellohasan/networkcallwithretrofit/Activity/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.hellohasan.networkcallwithretrofit.Activity; 2 | 3 | import androidx.annotation.NonNull; 4 | import androidx.appcompat.app.AppCompatActivity; 5 | import android.os.Bundle; 6 | import android.util.Log; 7 | import android.view.View; 8 | import android.widget.EditText; 9 | import android.widget.TextView; 10 | import android.widget.Toast; 11 | 12 | import com.hellohasan.networkcallwithretrofit.Interface.ApiInterface; 13 | import com.hellohasan.networkcallwithretrofit.Model.User; 14 | import com.hellohasan.networkcallwithretrofit.Model.ServerResponse; 15 | import com.hellohasan.networkcallwithretrofit.R; 16 | import com.hellohasan.networkcallwithretrofit.Retrofit.RetrofitApiClient; 17 | 18 | 19 | import retrofit2.Call; 20 | import retrofit2.Callback; 21 | import retrofit2.Response; 22 | 23 | public class MainActivity extends AppCompatActivity { 24 | 25 | private static final String TAG = MainActivity.class.getSimpleName(); 26 | private ApiInterface apiInterface; 27 | private EditText userIdEditText; 28 | private EditText passwordEditText; 29 | private EditText jokeUserIdEditText; 30 | private TextView jokeTextView; 31 | 32 | @Override 33 | protected void onCreate(Bundle savedInstanceState) { 34 | super.onCreate(savedInstanceState); 35 | setContentView(R.layout.activity_main); 36 | 37 | //Create an instance of Interface 38 | apiInterface = RetrofitApiClient.getClient().create(ApiInterface.class); 39 | 40 | //Initialize the view like EditText, TextView 41 | viewInitialization(); 42 | } 43 | 44 | // Login button event 45 | public void buttonClickEvent(View view){ 46 | 47 | if(view.getId()==R.id.login_button){ 48 | String userId; 49 | String password; 50 | User user = new User(); 51 | 52 | userId = userIdEditText.getText().toString(); 53 | password = passwordEditText.getText().toString(); 54 | 55 | user.setUserId(userId); 56 | user.setPassword(password); 57 | 58 | checkUserValidity(user); 59 | } else { 60 | String userId; 61 | 62 | userId = jokeUserIdEditText.getText().toString(); 63 | 64 | getJokeFromServer(userId); 65 | } 66 | 67 | } 68 | 69 | // GET method to get a Joke from remote server 70 | private void getJokeFromServer(String userId) { 71 | 72 | Call call = apiInterface.getJoke(userId); 73 | 74 | call.enqueue(new Callback() { 75 | @Override 76 | public void onResponse(@NonNull Call call, @NonNull Response response) { 77 | ServerResponse validity = response.body(); 78 | if (validity != null) 79 | jokeTextView.setText(validity.getMessage()); 80 | else 81 | jokeTextView.setText("Server response is null"); 82 | } 83 | 84 | @Override 85 | public void onFailure(@NonNull Call call, @NonNull Throwable t) { 86 | Log.e(TAG, t.toString()); 87 | } 88 | }); 89 | } 90 | 91 | // POST method to determine user validity 92 | private void checkUserValidity(User userCredential){ 93 | 94 | Call call = apiInterface.getUserValidity(userCredential); 95 | 96 | call.enqueue(new Callback() { 97 | 98 | @Override 99 | public void onResponse(@NonNull Call call, @NonNull Response response) { 100 | 101 | ServerResponse validity = response.body(); 102 | 103 | if (validity != null) 104 | Toast.makeText(getApplicationContext(), validity.getMessage(), Toast.LENGTH_LONG).show(); 105 | else 106 | Toast.makeText(getApplicationContext(), "Server response is null", Toast.LENGTH_LONG).show(); 107 | } 108 | 109 | @Override 110 | public void onFailure(@NonNull Call call, @NonNull Throwable t) { 111 | Log.e(TAG, t.toString()); 112 | } 113 | }); 114 | } 115 | 116 | private void viewInitialization() { 117 | userIdEditText = findViewById(R.id.login_id); 118 | passwordEditText = findViewById(R.id.login_password); 119 | jokeUserIdEditText = findViewById(R.id.user_id_for_joke); 120 | jokeTextView = findViewById(R.id.jokeTextView); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /Android Code/app/src/main/java/com/hellohasan/networkcallwithretrofit/Interface/ApiInterface.java: -------------------------------------------------------------------------------- 1 | package com.hellohasan.networkcallwithretrofit.Interface; 2 | 3 | import com.hellohasan.networkcallwithretrofit.Model.User; 4 | import com.hellohasan.networkcallwithretrofit.Model.ServerResponse; 5 | 6 | import retrofit2.Call; 7 | import retrofit2.http.Body; 8 | import retrofit2.http.GET; 9 | import retrofit2.http.POST; 10 | import retrofit2.http.Query; 11 | 12 | public interface ApiInterface { 13 | 14 | @POST("/retrofit_get_post/server_side_code.php") 15 | Call getUserValidity(@Body User userLoginCredential); 16 | 17 | @GET("/retrofit_get_post/server_side_code.php") 18 | Call getJoke(@Query("user_id") String userId); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Android Code/app/src/main/java/com/hellohasan/networkcallwithretrofit/Model/ServerResponse.java: -------------------------------------------------------------------------------- 1 | package com.hellohasan.networkcallwithretrofit.Model; 2 | 3 | 4 | import com.google.gson.annotations.SerializedName; 5 | 6 | public class ServerResponse { 7 | 8 | @SerializedName("status") 9 | boolean statusString; 10 | @SerializedName("message") 11 | String messageString; 12 | 13 | public boolean isSuccess(){ 14 | return statusString; 15 | } 16 | 17 | public String getMessage() { 18 | return messageString; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Android Code/app/src/main/java/com/hellohasan/networkcallwithretrofit/Model/User.java: -------------------------------------------------------------------------------- 1 | package com.hellohasan.networkcallwithretrofit.Model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | public class User { 6 | 7 | @SerializedName("user_id") 8 | private String userId; 9 | @SerializedName("password") 10 | private String password; 11 | 12 | public User(){} 13 | 14 | public void setUserId(String userId) { 15 | this.userId = userId; 16 | } 17 | 18 | public void setPassword(String password) { 19 | this.password = password; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Android Code/app/src/main/java/com/hellohasan/networkcallwithretrofit/Retrofit/RetrofitApiClient.java: -------------------------------------------------------------------------------- 1 | package com.hellohasan.networkcallwithretrofit.Retrofit; 2 | 3 | import com.google.gson.Gson; 4 | import com.google.gson.GsonBuilder; 5 | import retrofit2.Retrofit; 6 | import retrofit2.converter.gson.GsonConverterFactory; 7 | 8 | public class RetrofitApiClient { 9 | 10 | private static final String BASE_URL = "http://192.168.0.104"; //address of your remote server. Here I used localhost 11 | private static Retrofit retrofit = null; 12 | 13 | private static Gson gson = new GsonBuilder() 14 | .setLenient() 15 | .create(); 16 | 17 | private RetrofitApiClient() {} // So that nobody can create an object with constructor 18 | 19 | public static synchronized Retrofit getClient() { 20 | if (retrofit==null) { 21 | retrofit = new Retrofit.Builder() 22 | .baseUrl(BASE_URL) 23 | .addConverterFactory(GsonConverterFactory.create(gson)) 24 | .build(); 25 | } 26 | return retrofit; 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /Android Code/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 20 | 21 | 28 | 29 |