Upload
faren-faren
View
666
Download
0
Embed Size (px)
Citation preview
AwesomeAndroid 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();
}
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);}
“Don’t reinvent the wheel”
— Naive Man
Butterknife
Retrofit (Network)Picasso (Image download and cache handler)
RoboSpice (Asynchronous lib)
Lombok
RetroLambda
Libs:
Never again:HttpConnection
Retrofit
➢ Model (POJO)
➢ Interface Service
➢ Adapter / Call
➢ Converter such as GSON
Retrofit
public class Question { private String question; private String url;
public String getQuestion() { return question;}
public String getUrl() { return url;}}
Retrofit (Model POJO)
public interface RetrofitRequestService {
@GET("/questions")
Call<List<Question>> ques();
}
Retrofit (Service Interface)
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)
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)
“Simple, it is wrapped”
— Naive Man
https://github.com/faren/Retrofit
https://id.linkedin.com/in/farenfaren
http://slideshare.net/farenfa
attr