Json parsing using Retrofit in Android with login parameters.json parsing has been done with Post method
Full project with source code of Json parsing using Retrofit in Android
buidle.gradle-1
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.3"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle-2
plugins {
id 'com.android.application'
}
android {
compileSdk 30
defaultConfig {
applicationId "tech.softbinarycrunch.retrofit"
minSdk 21
targetSdk 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'androidx.navigation:navigation-fragment:2.3.5'
implementation 'androidx.navigation:navigation-ui:2.3.5'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.9.0'
}
MainActivity.java
package tech.softbinarycrunch.retrofit;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;
public class MainActivity extends AppCompatActivity {
RelativeLayout rl;LinearLayout ll;EditText et1,et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rl=new RelativeLayout(getApplicationContext());
RelativeLayout.LayoutParams rlp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
rl.setLayoutParams(rlp);
ll=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams llp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(llp);
ll.setGravity(Gravity.CENTER);
ll.setOrientation(LinearLayout.VERTICAL);
et=new EditText(getApplicationContext());
et.setHint("user name");
ll.addView(et);
et1=new EditText(getApplicationContext());
et1.setHint("password");
ll.addView(et1);
rl.addView(ll);
Button bt=new Button(getApplicationContext());
bt.setText("ok");
ll.addView(bt);
setContentView(rl);
listener(bt,et.getText().toString(),et1.getText().toString(),rl);
}
private void message(String s){
new Handler(getApplicationContext().getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
}
});
}
private void listener(Button bt,String s1,String s2,RelativeLayout rl){
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(et.getText().toString().length()==0){
message("user name is empty");
return ;
}
if(et1.getText().toString().length()==0){
message("passwword is empty");
return;
}
loginUser(s1,s2,rl);
}
});
}
private void loginUser(String s1,String s2,RelativeLayout rl) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(PostInterface.JSONURL)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
PostInterface api = retrofit.create(PostInterface.class);
Call<String> call = api.getUserLogin(s1,s2,"interview@maishainfotech.com");
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
Log.i("Responsestring", response.body().toString());
//Toast.makeText()
if (response.isSuccessful()) {
if (response.body() != null) {
Log.i("onSuccess", response.body().toString());
String jsonresponse = response.body().toString();
parseLoginData(jsonresponse);
} else {
Log.i("onEmptyResponse", "Returned empty response");
}
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
public void parseLoginData(String response) {
System.out.println("response"+response);
try {
JSONObject jsonObject = new JSONObject(response);
gridlauout(jsonObject);
/*if (jsonObject.getString("status").equals("true")) {
JSONArray dataArray = jsonObject.getJSONArray("data");
for (int i = 0; i < dataArray.length(); i++) {
}
}*/
} catch (JSONException e) {
e.printStackTrace();
}
}
private void gridlauout(JSONObject jsonObject){
GridLayout gridLayout=new GridLayout(getApplicationContext());
gridLayout.setColumnCount(2);
gridLayout.setRowCount(jsonObject.length());
gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
for(int i=0;i<jsonObject.length();i++){
Button bt=new Button(getApplicationContext());
Button btt=new Button(getApplicationContext());
try {
btt.setText("response");
bt.setText(jsonObject.getString("response"));
}
catch (Exception e){
e.printStackTrace();
}
gridLayout.addView(btt);
gridLayout.addView(bt);
}
ll.addView(gridLayout);
}
}