14
Awesome Android Network Library

Android Network library

Embed Size (px)

Citation preview

Page 1: Android Network library

AwesomeAndroid Network Library

Page 2: Android Network library

Old Times - Connect HTTP ProtocolHttpParams httpParameters = new BasicHttpParams();HttpConnectionParams.setConnectionTimeout(httpParameters, 10000); HttpConnectionParams.setSoTimeout(httpParameters, 5000);

HttpClient client = new DefaultHttpClient(httpParameters);HttpGet post = new HttpGet(urlString);HttpResponse responseGET;try {

responseGET = client.execute(post);HttpEntity resEntity = responseGET.getEntity();

if (resEntity != null) {String resEntityString =

EntityUtils.toString(resEntity);

return resEntityString;}

} catch (Exception e) {e.printStackTrace();

}

Page 3: Android Network library

Old Times - JSON ConverterJSONObject jsonObject = new JSONObject(data);String questionId = jsonObject.getString(PARAMETER1);String questionName = jsonObject.getString(PARAMETER2);String questionOther = jsonObject.getString(PARAMETER3);

JSONArray jarr = new JSONArray(jsonObject.getString(JSON_CONTENT))ArrayList<ChoiceModel> choiceItem = new ArrayList<ChoiceItemModel>();for (int i = 0; i < jarr.length(); i++){ JSONObject jo = jarr.getJSONObject(i);

ChoiceItemModel item = new ChoiceItemModel();item.setQuestionId(questionId);item.setChoiceName(jo.getString(JSON_CHOICE));item.setChoiceOther(jo.getInt(JSON_CHOICE_OTHER));

surveyItem.add(item);}

Page 4: Android Network library

“Don’t reinvent the wheel”

— Naive Man

Page 5: Android Network library

Butterknife

Retrofit (Network)Picasso (Image download and cache handler)

RoboSpice (Asynchronous lib)

Lombok

RetroLambda

Libs:

Page 6: Android Network library

Never again:HttpConnection

Retrofit

Page 7: Android Network library

➢ Model (POJO)

➢ Interface Service

➢ Adapter / Call

➢ Converter such as GSON

Retrofit

Page 8: Android Network library

public class Question { private String question; private String url;

public String getQuestion() { return question;}

public String getUrl() { return url;}}

Retrofit (Model POJO)

Page 9: Android Network library

public interface RetrofitRequestService {

@GET("/questions")

Call<List<Question>> ques();

}

Retrofit (Service Interface)

Page 10: Android Network library

Retrofit mRestAdapter = new Retrofit

.Builder()

.baseUrl("http://private-9adb36-crashback.apiary-mock.com/")

.addConverterFactory(GsonConverterFactory.create())

.build();

service = mRestAdapter.create(RetrofitRequestService.class);

Retrofit (Adapter / Call)

Page 11: Android Network library

Call<List<Question>> call = service.ques();

//call on another thread not in main threadcall.enqueue(new Callback<List<Question>>() { @Override public void onResponse(Call<List<Question>> call, Response<List<Question>> response) { System.out.println("Question = " + response.body().get(0).getQuestion()); System.out.println("url = " + response.body().get(0).getUrl()); }

@Override public void onFailure(Call<List<Question>> call, Throwable t) { }});

Retrofit (Adapter / Call)

Page 12: Android Network library

“Simple, it is wrapped”

— Naive Man

Page 13: Android Network library

https://github.com/faren/Retrofit

https://id.linkedin.com/in/farenfaren

http://slideshare.net/farenfa

attr

Page 14: Android Network library

[email protected]

Dev Manager Tiket.com

Ping me