Ad Code

Responsive Advertisement

Query Api

 

ApiInterface.java

package com.example.android007;

import com.example.android007.model.ModelData;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface ApiInter {

@GET("getVideos.php")
Call<ModelData> getData(@Query("type")String type,@Query("start-index")String index,@Query("max-results")String res,@Query("language20%20id")String lan);
}



ApiClient.java

package com.example.android007;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {

public static String BASEURL = "https://api.indiatvshowz.com/v1/";
public static Retrofit retrofit = null;

public static Retrofit getRetrofit() {

if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASEURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}

return retrofit;
}
}


MainActivity.class

package com.example.android007;

import static com.example.android007.ApiClient.getRetrofit;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.android007.model.Datum;
import com.example.android007.model.ModelData;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

private Button btn;
private RecyclerView rv_data;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv_data =findViewById(R.id.rv_data);

btn =findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
apiCalling();
}
});
}

void apiCalling()
{
ApiInter apiInter = ApiClient.getRetrofit().create(ApiInter.class);
apiInter.getData("song","1","20","1").enqueue(new Callback<ModelData>() {
@Override
public void onResponse(Call<ModelData> call, Response<ModelData> response) {
ModelData m1 =response.body();


List<Datum> l1 = m1.getData();
rv(l1);
Log.e("TAG", "onResponse: ===== "+m1.getData().get(0).getVideoTitle() );
Log.e("TAG", "onResponse: ===== "+m1.getData().get(0).getThumbUrl() );
}

@Override
public void onFailure(Call<ModelData> call, Throwable t) {
Toast.makeText(MainActivity.this, ""+t.getMessage(), Toast.LENGTH_SHORT).show();
Log.e("TAG", "onFailure: "+t.getMessage() );
}
});
}

void rv(List<Datum> l1)
{
MyAdapter adapter = new MyAdapter(MainActivity.this,l1);
RecyclerView.LayoutManager lm = new LinearLayoutManager(MainActivity.this);
rv_data.setLayoutManager(lm);
rv_data.setAdapter(adapter);

}
}




activitymain.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:gravity="center"
android:orientation="vertical"
android:layout_height="match_parent">

<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="200dp"/>


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ANC"
android:id="@+id/txt"/>


</LinearLayout>



















Post a Comment

0 Comments

Ad Code

Responsive Advertisement