Design, of course, does not shine
dependencies
section: compile 'com.squareup.retrofit2:retrofit:2.1.0'
<dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>retrofit</artifactId> <version>2.1.0</version> </dependency>
compile 'com.squareup.retrofit2:converter-gson:2.1.0' // JSON, , , Jackson compile 'com.android.support:recyclerview-v7:25.0.0' //RecyclerView
https://api.github.com/users/octocat/repos
, where:https://umorili.herokuapp.com/api/get?name=bash&num=50
, where name=bash&num=50
are parameters. package ru.mustakimov.retrofittutorial.api; import java.util.List; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Query; import ru.mustakimov.retrofittutorial.PostModel; public interface UmoriliApi { @GET("/api/get") Call<List<PostModel>> getData(@Query("name") String resourceName, @Query("num") int count); }
Call<List<PostModel>>
. Methods should always return an object of type Call<T>
and have an annotation of the type of request (GET, POST, PUT, DELETE).@Query("name") String resourceName
shows the Retrofit that you need to set the name = <String resourceName> value as the query parameter.@Path("< >") SomeType variable
, where SomeType is any type (for example, String, int, float). package ru.mustakimov.retrofittutorial; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; public class PostModel { @SerializedName("site") @Expose private String site; @SerializedName("name") @Expose private String name; @SerializedName("desc") @Expose private String desc; @SerializedName("link") @Expose private String link; @SerializedName("elementPureHtml") @Expose private String elementPureHtml; /** * @return The site */ public String getSite() { return site; } /** * @param site The site */ public void setSite(String site) { this.site = site; } /** * @return Site name */ public String getName() { return name; } /** * @param name Site name */ public void setName(String name) { this.name = name; } /** * @return Site description */ public String getDesc() { return desc; } /** * @param desc Site description */ public void setDesc(String desc) { this.desc = desc; } /** * @return The link */ public String getLink() { return link; } /** * @param link The link */ public void setLink(String link) { this.link = link; } /** * @return The elementPureHtml */ public String getElementPureHtml() { return elementPureHtml; } /** * @param elementPureHtml The elementPureHtml */ public void setElementPureHtml(String elementPureHtml) { this.elementPureHtml = elementPureHtml; } }
package ru.mustakimov.retrofittutorial; import android.app.Application; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; import ru.mustakimov.retrofittutorial.api.UmoriliApi; public class App extends Application { private static UmoriliApi umoriliApi; private Retrofit retrofit; @Override public void onCreate() { super.onCreate(); retrofit = new Retrofit.Builder() .baseUrl("https://umorili.herokuapp.com") // .addConverterFactory(GsonConverterFactory.create()) //, JSON' .build(); umoriliApi = retrofit.create(UmoriliApi.class); // , } public static UmoriliApi getApi() { return umoriliApi; } }
PS do not forget to register in the manifest that we use our Application classexecute()
method on an object of type Call. For our example, the code would be as follows: Response response = App.getApi().getData("bash", 50).execute();
body()
method.execute()
with enqueue()
, where in the parameters we pass callback functions (callbacks). In our example, it will look something like this: App.getApi().getData("bash", 50).enqueue(new Callback<List<PostModel>>() { @Override public void onResponse(Call<List<PostModel>> call, Response<List<PostModel>> response) { // , response.body() null } @Override public void onFailure(Call<List<PostModel>> call, Throwable t) { // } });
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="ru.mustakimov.retrofittutorial.MainActivity"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:id="@+id/posts_recycle_view" android:layout_alignParentStart="true" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:padding="5dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/postitem_post" android:layout_width="match_parent" android:layout_height="wrap_content" android:text=" , , " android:textColor="?android:attr/textColorPrimary" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <TextView android:id="@+id/postitem_site" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bash.im" android:layout_below="@+id/postitem_post" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:gravity="end" android:textAlignment="textEnd" /> </RelativeLayout>
package ru.mustakimov.retrofittutorial; import android.os.Build; import android.support.v7.widget.RecyclerView; import android.text.Html; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.List; public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.ViewHolder> { private List<PostModel> posts; public PostsAdapter(List<PostModel> posts) { this.posts = posts; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_item, parent, false); return new ViewHolder(v); } @Override public void onBindViewHolder(ViewHolder holder, int position) { PostModel post = posts.get(position); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { holder.post.setText(Html.fromHtml(post.getElementPureHtml(), Html.FROM_HTML_MODE_LEGACY)); } else { holder.post.setText(Html.fromHtml(post.getElementPureHtml())); } holder.site.setText(post.getSite()); } @Override public int getItemCount() { if (posts == null) return 0; return posts.size(); } class ViewHolder extends RecyclerView.ViewHolder { TextView post; TextView site; public ViewHolder(View itemView) { super(itemView); post = (TextView) itemView.findViewById(R.id.postitem_post); site = (TextView) itemView.findViewById(R.id.postitem_site); } } }
package ru.mustakimov.retrofittutorial; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.widget.Toast; import java.io.IOException; import java.util.ArrayList; import java.util.List; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; public class MainActivity extends AppCompatActivity { RecyclerView recyclerView; List<PostModel> posts; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); posts = new ArrayList<>(); recyclerView = (RecyclerView) findViewById(R.id.posts_recycle_view); LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); PostsAdapter adapter = new PostsAdapter(posts); recyclerView.setAdapter(adapter); try { Response response = App.getApi().getData("bash", 50).execute(); } catch (IOException e) { e.printStackTrace(); } App.getApi().getData("bash", 50).enqueue(new Callback<List<PostModel>>() { @Override public void onResponse(Call<List<PostModel>> call, Response<List<PostModel>> response) { posts.addAll(response.body()); recyclerView.getAdapter().notifyDataSetChanged(); } @Override public void onFailure(Call<List<PostModel>> call, Throwable t) { Toast.makeText(MainActivity.this, "An error occurred during networking", Toast.LENGTH_SHORT).show(); } }); } }
Source: https://habr.com/ru/post/314028/
All Articles