Dependency: (In gradle file)
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
implementation 'com.squareup.retrofit2:retrofit:2.6.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
Manifest
<uses-permission android:name="android.permission.INTERNET"/>
Api client java file.
class APIClient {
private static Retrofit retrofit = null;
static Retrofit getClient() {
//
// HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
// interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
// OkHttpClientttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
// .baseUrl("https://reqres.in")
.baseUrl("https://jsonplaceholder.typicode.com/")
.addConverterFactory(GsonConverterFactory.create())
// .client(client)
.build();
return retrofit;
}
}
**********************************************************************
Api Interface
interface APIInterface {
@GET("/comments")
Call<ArrayList<Comments>> getComments();
}
***********************************************************************
Model class
public class Comments {
private long postID;
private long id;
private String name;
private String email;
private String body;
public long getPostID() { return postID; }
public void setPostID(long value) { this.postID = value; }
public long getID() { return id; }
public void setID(long value) { this.id = value; }
public String getName() { return name; }
public void setName(String value) { this.name = value; }
public String getEmail() { return email; }
public void setEmail(String value) { this.email = value; }
public String getBody() { return body; }
public void setBody(String value) { this.body = value; }
}
*******************************************************************************
public class MainActivity extends AppCompatActivity {
APIInterface apiInterface;
RecyclerView mList;
MyRecyclerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mList = findViewById(R.id.list);
apiInterface = APIClient.getClient().create(APIInterface.class);
Call<ArrayList<Comments>> c = apiInterface.getComments();
c.enqueue(new Callback<ArrayList<Comments>>() {
@Override
public void onResponse(Call<ArrayList<Comments>> call, Response<ArrayList<Comments>> response) {
Log.e("data",response.body().toString());
adapter = new MyRecyclerAdapter(MainActivity.this,response.body());
mList.setAdapter(adapter);
}
@Override
public void onFailure(Call<ArrayList<Comments>> call, Throwable t) {
}
});
}
}
*******************************************************************************
Recycler adapter (in java file)
package com.example.retrofitex.Adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.retrofitex.R;
import com.example.retrofitex.pojo.Comments;
import java.util.ArrayList;
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {
LayoutInflater inflater;
ArrayList<Comments> allItems;
Context context;
public MyRecyclerAdapter(Context context,ArrayList<Comments> comments){
inflater = LayoutInflater.from(context);
this.context = context;
this.allItems = comments;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = inflater.inflate(R.layout.adapter_view,viewGroup,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder,
final int i) {
Comments c = allItems.get(i);
holder.mName.setText(c.getName()+"");
}
@Override
public int getItemCount() {
return allItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView mName;
public ViewHolder(@NonNull View view) {
super(view);
this.mName = view.findViewById(R.id.name);
}
}
}
******************************************************************************
Adapter view ..
<?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="wrap_content"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:padding="10dp"
/>
</LinearLayout>
<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="wrap_content"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:padding="10dp"
/>
</LinearLayout>
*******************************************************************************
activity_main.xml
<?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"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/adapter_view"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:id="@+id/list"
/>
</LinearLayout>
<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"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/adapter_view"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:id="@+id/list"
/>
</LinearLayout>
Exployer
Gradle Dependency :
implementation 'com.google.android.exoplayer:exoplayer:2.8.4'
Manifest :
Internet Permission
Layout file :
<?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">
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:layout_width="match_parent"
app:resize_mode="fill"
android:layout_height="match_parent"
android:id="@+id/view"
/>
</android.support.constraint.ConstraintLayout>
*****************************************************************************************
Main java file
*****************************************************************************************
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.extractor.ExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.ui.SimpleExoPlayerView;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
public class MainActivity extends AppCompatActivity {
private SimpleExoPlayerView simpleExoPlayerView;
private String hlsVideoUri = "https://www.radiantmediaplayer.com/media/bbb-360p.mp4";
private SimpleExoPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleExoPlayerView = findViewById(R.id.view);
try{
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
player = ExoPlayerFactory.newSimpleInstance(this,trackSelector);
Uri videoUri = Uri.parse(hlsVideoUri);
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer_video");
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(videoUri,dataSourceFactory,extractorsFactory,null,null);
simpleExoPlayerView.setPlayer(player);
player.prepare(mediaSource);
player.setPlayWhenReady(true);
}catch (Exception e){
Log.e("error",Log.getStackTraceString(e));
}
}
}