Retrofit_library_sir
build.gradle(Module:app)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.squareup.picasso:picasso:2.71828'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
}
step 1:Activity_main.xml //listView j levu
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="demoproject.aalap.com.retrofit.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"></ListView>
</LinearLayout>
step 2:Simple.xml //imageview and textView levu and aene id aapi deve url ma image hoy to j image view levu baki eklu text view lai levu
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv"
android:background="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
step 3:Api.java
//aama java no new class bnave tyare inflace class levo
//"https://simplifiedcoding.net/demos/marvel" google ma serech aa url karvi retrofit ma je url ma last je hoy ae @GET ma lkhvanu ex.aama url ma last marvel last che to marvel GET ma lkeluu che
package demoproject.aalap.com.retrofit;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
/**
* Created by ADMIN on 11-06-2018.
*/
public interface Api {
String BASE_URL = "https://simplifiedcoding.net/demos/";
@GET("marvel")
Call<List<Hero>> getHeroes();
}
Hero.java//hero class ie pojo class jema get and set tay
package demoproject.aalap.com.retrofit;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
/**
* Created by ADMIN on 11-06-2018.
*/
public class Hero {
private String name;
private String realname;
private String team;
private String firstappearance;
private String createdby;
private String publisher;
private String imageurl;
private String bio;
//aatlu lkhiya pache righ click karvu and generate ma jve anu and gater and sater pr click karvu
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getTeam() {
return team;
}
public void setTeam(String team) {
this.team = team;
}
public String getFirstappearance() {
return firstappearance;
}
public void setFirstappearance(String firstappearance) {
this.firstappearance = firstappearance;
}
public String getCreatedby() {
return createdby;
}
public void setCreatedby(String createdby) {
this.createdby = createdby;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getImageurl() {
return imageurl;
}
public void setImageurl(String imageurl) {
this.imageurl = imageurl;
}
public String getBio() {
return bio;
}
public void setBio(String bio) {
this.bio = bio;
}
}
step 4:jsonAdapter.java
package demoproject.aalap.com.retrofit;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
/**
* Created by ADMIN on 11-06-2018.
*/
public class jsonAdapter extends BaseAdapter {
Context context;
ArrayList<Hero> arryList=new ArrayList<>();
public jsonAdapter(Context context, ArrayList<Hero> arryList) {
this.context = context;
this.arryList = arryList;
}
@Override
public int getCount() {
return arryList.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View v=inflater.inflate(R.layout.simple,null);
ImageView imageView;
TextView textView;
imageView=v.findViewById(R.id.iv);
textView=v.findViewById(R.id.tv);
Hero h=arryList.get(i);
textView.setText(h.getName() +"\n"+ h.getRealname() +"\n"+h.getTeam() +"\n"+ h.getFirstappearance() +"\n"+h.getCreatedby() +"\n"+ h.getPublisher() +"\n"+h.getBio());
Picasso.get().load(h.getImageurl()).into(imageView);
return v;
}
}
step 5:MainActivity.java
package demoproject.aalap.com.retrofit;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
//calling the method to display the heroes
getHeroes();
}
private void getHeroes() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
Api api = retrofit.create(Api.class);
Call<List<Hero>> call = api.getHeroes();
call.enqueue(new Callback<List<Hero>>() {
@Override
public void onResponse(Call<List<Hero>> call, Response<List<Hero>> response) {
List<Hero> heroList = response.body();
ArrayList<Hero> list = new ArrayList<>();
for (int i = 0; i < heroList.size(); i++) {
Hero h1 = new Hero();
h1.setName(heroList.get(i).getName());
h1.setBio(heroList.get(i).getBio());
h1.setRealname(heroList.get(i).getRealname());
h1.setTeam(heroList.get(i).getTeam());
h1.setFirstappearance(heroList.get(i).getFirstappearance());
h1.setCreatedby(heroList.get(i).getCreatedby());
h1.setPublisher(heroList.get(i).getPublisher());
list.add(h1);
}
jsonAdapter adp = new jsonAdapter(MainActivity.this,list);
lv.setAdapter(adp);
}
@Override
public void onFailure(Call<List<Hero>> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
Comments
Post a Comment