use of retrofit2.Callback in project gce2retrofit by chiuki.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TextView textView = new TextView(this);
setContentView(textView);
Retrofit restAdapter = new Retrofit.Builder().baseUrl(API_URL).addConverterFactory(GsonConverterFactory.create()).build();
Apis apis = restAdapter.create(Apis.class);
Call<DirectoryList> call = apis.list(null, null);
call.enqueue(new Callback<DirectoryList>() {
@Override
public void onResponse(Call<DirectoryList> call, Response<DirectoryList> response) {
textView.setText(response.body().kind);
}
@Override
public void onFailure(Call<DirectoryList> call, Throwable t) {
String msg = t.getMessage();
if (TextUtils.isEmpty(msg)) {
textView.setText(t.getClass().getSimpleName());
}
}
});
}
use of retrofit2.Callback in project BBS-Android by bdpqchen.
the class CollectionClient method deleteCollection.
void deleteCollection(int tid) {
Retrofit retrofit = new Retrofit.Builder().client(client).addConverterFactory(GsonConverterFactory.create()).baseUrl("https://bbs.twtstudio.com/api/home/").build();
CollectionApi collectionApi = retrofit.create(CollectionApi.class);
Call<SimpleBean> call = collectionApi.deleteCollection(uidToken, String.valueOf(tid));
call.enqueue(new Callback<SimpleBean>() {
@Override
public void onResponse(Call<SimpleBean> call, Response<SimpleBean> response) {
collectionPresenter.dealDeleteData(response.body());
}
@Override
public void onFailure(Call<SimpleBean> call, Throwable t) {
}
});
}
use of retrofit2.Callback in project MVCHelper by LuckyJayce.
the class BooksOkHttpNormal_DataSource method loadBooks.
private RequestHandle loadBooks(final ResponseSender<List<Book>> sender, final int page) throws Exception {
// 这里只是简单的演示OKhttp,你可以再封装哦
FormBody.Builder formEncodingBuilder = new FormBody.Builder();
formEncodingBuilder.add("api_key", "75ee6c644cad38dc8e53d3598c8e6b6c");
Request request = new Request.Builder().url("https://www.baidu.com").post(formEncodingBuilder.build()).build();
Call call = OkHttpUtils.client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// send 要放在最后一句(请求结束)
sender.sendError(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final List<Book> books = new ArrayList<Book>();
for (int i = 0; i < 15; i++) {
books.add(new Book("page" + page + " Java编程思想 " + i, 108.00d));
}
mPage = page;
// send 要放在最后一句(请求结束)
sender.sendData(books);
}
});
return new OKHttpRequestHandle(call);
}
use of retrofit2.Callback in project iNGAGE by davis123123.
the class UserRecentCommentHandler method enqueue.
// get all recent comments for each room, for the user
public void enqueue(String username) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
Retrofit retrofit = new Retrofit.Builder().client(httpClient.build()).addConverterFactory(GsonConverterFactory.create()).baseUrl(get_url).build();
Interface service = retrofit.create(Interface.class);
Call<ResponseBody> call = service.get(username);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.i("STATE", "Retrofit response code: " + response.code());
if (response.isSuccessful()) {
Log.i("STATE", "Retrofit POST Success");
try {
serverResponse = response.body().string();
createCommentsList(serverResponse);
callBackData.notifyChange();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Log.i("Retrofit Error Code:", String.valueOf(response.code()));
Log.i("Retrofit Error Body", response.errorBody().toString());
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.i("STATE", "Retrofit Failure");
}
});
}
use of retrofit2.Callback in project WeexErosFramework by bmfe.
the class DefaultWXHttpAdapter method fetchUrl.
private void fetchUrl(final WXRequest request, final OnHttpListener listener) {
final WXResponse wxResponse = new WXResponse();
String method = request.method == null ? "GET" : request.method.toUpperCase();
String requestBodyString = request.body == null ? "{}" : request.body;
RequestBody body = null;
if (request.paramMap != null && request.paramMap.containsKey("Content-Type")) {
body = HttpMethod.requiresRequestBody(method) ? RequestBody.create(MediaType.parse(request.paramMap.get("Content-Type")), requestBodyString) : null;
} else {
body = HttpMethod.requiresRequestBody(method) ? RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=UTF-8"), requestBodyString) : null;
}
Request.Builder requestBuilder = new Request.Builder().url(request.url).method(method, body);
if (request.paramMap != null) {
for (Map.Entry<String, String> param : request.paramMap.entrySet()) {
requestBuilder.addHeader(param.getKey(), TextUtil.toHumanReadableAscii(param.getValue()));
}
}
client.newCall(requestBuilder.build()).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
wxResponse.errorMsg = e.getMessage();
wxResponse.errorCode = "-1";
wxResponse.statusCode = "-1";
if (listener != null) {
listener.onHttpFinish(wxResponse);
}
}
@Override
public void onResponse(Call call, Response response) {
byte[] responseBody = new byte[0];
try {
responseBody = response.body().bytes();
} catch (IOException e) {
e.printStackTrace();
wxResponse.errorMsg = e.getMessage();
wxResponse.errorCode = "-1";
wxResponse.statusCode = "-1";
if (listener != null) {
listener.onHttpFinish(wxResponse);
}
}
wxResponse.data = new String(responseBody);
wxResponse.statusCode = String.valueOf(response.code());
wxResponse.originalData = responseBody;
wxResponse.extendParams = new HashMap<>();
for (Map.Entry<String, List<String>> entry : response.headers().toMultimap().entrySet()) {
wxResponse.extendParams.put(entry.getKey(), entry.getValue());
}
if (response.code() < 200 || response.code() > 299) {
wxResponse.errorMsg = response.message();
if (listener != null) {
listener.onHttpFinish(wxResponse);
}
} else {
if (isInterceptor(request.url)) {
appendBaseJs(wxResponse, listener);
// listener);
if (listener != null) {
listener.onHttpFinish(wxResponse);
}
} else {
// iconFont
if (listener != null) {
listener.onHttpFinish(wxResponse);
}
}
}
}
});
}
Aggregations