Android Retrofit Tutorial
Android Retrofit Example
Add Below CompileLine in module level "app.gradle" file
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
MainActivity.java
package android.social_integration.semwal.socialintegration.RecyclerViewExample;
import android.os.Bundle;
import android.social_integration.semwal.socialintegration.R;
import android.social_integration.semwal.socialintegration.RetrofitExample.APIService;
import android.social_integration.semwal.socialintegration.RetrofitExample.PojoClass;
import android.social_integration.semwal.socialintegration.RetrofitExample.Result;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.Retrofit;
/**
* Created by Semwal on 1/22/2018.
*/
public class RecycleActivity extends AppCompatActivity {
//Complete URl++==http://codenomad.biz/jason/driver_json.php/driverhistory
//keyId==driver_id=6;
RecyclerView recyclerView;
AdapterRecycle adapterRecycle;
String name[]={"amit","ram","sham"};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle);
recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
/*adapterRecycle=new AdapterRecycle(name);
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(RecycleActivity.this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapterRecycle);
*/
webServices();
}
List<Result> data=new ArrayList<>();
String[] nameData;
private void webServices(){
String BASE_URL="http://codenomad.biz/";
Retrofit retrofit=new Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create()).build();
APIService service=retrofit.create(APIService.class);
Call<PojoClass> call=service.pojoClass("6");
call.enqueue(new Callback<PojoClass>() {
@Override
public void onResponse(Call<PojoClass> call, Response<PojoClass> response) {
//data=new ArrayList<PojoClass>();
data=new ArrayList<>();
if (response.body().getCode().equals("201")) {
Log.d("Inside","Inside");
for (int i = 0; i < response.body().getResult().size(); i++) {
data.add(response.body().getResult().get(i));
adapterRecycle = new AdapterRecycle(data);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(RecycleActivity.this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapterRecycle);
}
}else {
Log.d("sddsdsd",response.body().getCode());
Log.d("sddsdsd",response.body().getText());
}
}
@Override
public void onFailure(Call<PojoClass> call, Throwable t) {
}
});
}
}
AdapterClass.Java
package android.social_integration.semwal.socialintegration.RecyclerViewExample;
import android.social_integration.semwal.socialintegration.R;
import android.social_integration.semwal.socialintegration.RetrofitExample.Result;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Semwal on 1/22/2018.
*/
public class AdapterRecycle extends RecyclerView.Adapter<AdapterRecycle.MyViewHolder> {
List<Result> text;
public AdapterRecycle(List<Result> name){
this.text=name;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
textView=(TextView)itemView.findViewById(R.id.textView);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_recyclerview,parent,false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Result result=text.get(position);
holder.textView.setText(result.getCustomerName());
}
@Override
public int getItemCount() {
return text.size();
}
}
Creat Interface calss for handling Get and Post the data
create interface "APIInterface"
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
/**
* Created by Semwal on 1/24/2018.
*/
public interface APIService {
@FormUrlEncoded
@POST("/jason/driver_json.php/driverhistory")
Call<PojoClass> pojoClass(@Field("driver_id") String id);
}
Create Xml files
activity_recycle.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</ScrollView>
</RelativeLayout>
adapter_recyclerview.xml
<?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="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"/>
</LinearLayout>
Post Image to Database Using Retrofit
First Convert image in File typeThen convert image file path in RequestBody
final RequestBody requestBody1 = RequestBody.create(MediaType.parse("*/*"), ImageFileFormet);
Then Convert requestBody in MultipartBody.Part
MultipartBody.Part Image = MultipartBody.Part.createFormData("databse_key", ImageFileFormet.getName(), requestBody1);
When you have to send text data with image in database then convert text into requestBody
RequestBody text = RequestBody.create(MediaType.parse("text/plain"), ""yourText);
Call webservice like this
new WebService().postData(Image,text);
getImage in WebService class like this/
public void postData(MultipartBody.Part Image,RequestBody text){
.
.
.
.
Call<SignUpResponse> call = webInterface.pojoClass(Image,text);
}
In InterFace use below code to send data
@Multipart
@POST("url/")
Call<PojoClass> pojoClass((@Part MultipartBody.Part image,
@Part("text_key") RequestBody text
);
Comments
Post a Comment