-
-
Save anandrex5/e283d27bbfb11ada802a03f4fecf0103 to your computer and use it in GitHub Desktop.
Retrofit Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
android:padding="8dp" | |
tools:context=".MainActivity"> | |
<android.support.v4.widget.NestedScrollView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:id="@+id/text_view_result" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textColor="#000" /> | |
</android.support.v4.widget.NestedScrollView> | |
</android.support.constraint.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.codinginflow.retrofitexample"> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codinginflow.retrofitexample; | |
import java.util.List; | |
import retrofit2.Call; | |
import retrofit2.http.GET; | |
public interface JsonPlaceHolderApi { | |
@GET("posts") | |
Call<List<Post>> getPosts(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codinginflow.retrofitexample; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.widget.TextView; | |
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 { | |
private TextView textViewResult; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
textViewResult = findViewById(R.id.text_view_result); | |
Retrofit retrofit = new Retrofit.Builder() | |
.baseUrl("https://jsonplaceholder.typicode.com/") | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build(); | |
JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class); | |
Call<List<Post>> call = jsonPlaceHolderApi.getPosts(); | |
call.enqueue(new Callback<List<Post>>() { | |
@Override | |
public void onResponse(Call<List<Post>> call, Response<List<Post>> response) { | |
if (!response.isSuccessful()) { | |
textViewResult.setText("Code: " + response.code()); | |
return; | |
} | |
List<Post> posts = response.body(); | |
for (Post post : posts) { | |
String content = ""; | |
content += "ID: " + post.getId() + "\n"; | |
content += "User ID: " + post.getUserId() + "\n"; | |
content += "Title: " + post.getTitle() + "\n"; | |
content += "Text: " + post.getText() + "\n\n"; | |
textViewResult.append(content); | |
} | |
} | |
@Override | |
public void onFailure(Call<List<Post>> call, Throwable t) { | |
textViewResult.setText(t.getMessage()); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codinginflow.retrofitexample; | |
import com.google.gson.annotations.SerializedName; | |
public class Post { | |
private int userId; | |
private int id; | |
private String title; | |
@SerializedName("body") | |
private String text; | |
public int getUserId() { | |
return userId; | |
} | |
public int getId() { | |
return id; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public String getText() { | |
return text; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add Dependecies inside build.gridle -
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
implementation 'com.google.code.gson:gson:2.6.2'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'