Search in sources :

Example 96 with Callback

use of zipkin2.Callback in project IITB-App by wncc.

the class ComplaintsHomeFragment method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_complaints_home, container, false);
    RecyclerView recyclerViewHome = view.findViewById(R.id.recyclerViewHome);
    homeListAdapter = new ComplaintsAdapter(getActivity(), uID, uProfileUrl);
    swipeContainer = view.findViewById(R.id.swipeContainer);
    error_message_home = view.findViewById(R.id.error_message_home);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    recyclerViewHome.setLayoutManager(llm);
    recyclerViewHome.setHasFixedSize(true);
    recyclerViewHome.setAdapter(homeListAdapter);
    recyclerViewHome.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (networkBusy || currentIndex == -1)
                return;
            if (!recyclerView.canScrollVertically(1)) {
                networkBusy = true;
                swipeContainer.setRefreshing(true);
                RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
                retrofitInterface.getAllComplaints(Utils.getSessionIDHeader(), currentIndex, 5).enqueue(new Callback<List<Venter.Complaint>>() {

                    @Override
                    public void onResponse(Call<List<Venter.Complaint>> call, Response<List<Venter.Complaint>> response) {
                        if (response.isSuccessful()) {
                            if (response.body() != null && !response.body().isEmpty()) {
                                complaints.addAll(response.body());
                                initialiseRecyclerView(complaints);
                                currentIndex += 5;
                            } else {
                                currentIndex = -1;
                            }
                        }
                        networkBusy = false;
                        swipeContainer.setRefreshing(false);
                    }

                    @Override
                    public void onFailure(Call<List<Venter.Complaint>> call, Throwable t) {
                        networkBusy = false;
                        swipeContainer.setRefreshing(false);
                    }
                });
            }
        }
    });
    swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            callServerToGetNearbyComplaints();
        }
    });
    swipeContainer.setColorSchemeResources(R.color.colorPrimary);
    if (!isCalled) {
        swipeContainer.post(new Runnable() {

            @Override
            public void run() {
                swipeContainer.setRefreshing(true);
                callServerToGetNearbyComplaints();
            }
        });
        isCalled = true;
    }
    return view;
}
Also used : Call(retrofit2.Call) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) TextView(android.widget.TextView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) ComplaintsAdapter(app.insti.adapter.ComplaintsAdapter) Response(retrofit2.Response) Callback(retrofit2.Callback) RecyclerView(androidx.recyclerview.widget.RecyclerView) RetrofitInterface(app.insti.api.RetrofitInterface)

Example 97 with Callback

use of zipkin2.Callback in project IITB-App by wncc.

the class BodyHeadViewHolder method bindView.

public void bindView(final Body body, final Fragment fragment) {
    /* Set body information */
    bodyName.setText(body.getBodyName());
    bodySubtitle.setText(body.getBodyShortDescription());
    /* Return if it's a min body */
    if (body.getBodyDescription() == null) {
        return;
    }
    Utils.getMarkwon().setMarkdown(bodyDescription, body.getBodyDescription());
    /* Check if user is already following
         * Initialize follow button */
    setupFollowButton(fragment.getContext(), body);
    followButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
            retrofitInterface.updateBodyFollowing(Utils.getSessionIDHeader(), body.getBodyID(), body.getBodyUserFollows() ? 0 : 1).enqueue(new Callback<Void>() {

                @Override
                public void onResponse(Call<Void> call, Response<Void> response) {
                    if (response.isSuccessful()) {
                        body.setBodyUserFollows(!body.getBodyUserFollows());
                        body.setBodyFollowersCount(body.getBodyUserFollows() ? body.getBodyFollowersCount() + 1 : body.getBodyFollowersCount() - 1);
                        setupFollowButton(fragment.getContext(), body);
                    }
                }

                @Override
                public void onFailure(Call<Void> call, Throwable t) {
                    Toast.makeText(fragment.getContext(), "Network Error", Toast.LENGTH_LONG).show();
                }
            });
        }
    });
    /* Initialize web button */
    if (body.getBodyWebsiteURL() != null && !body.getBodyWebsiteURL().isEmpty()) {
        webBodyButton.setVisibility(View.VISIBLE);
        webBodyButton.setOnClickListener(new View.OnClickListener() {

            String bodywebURL = body.getBodyWebsiteURL();

            @Override
            public void onClick(View view) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(bodywebURL));
                fragment.startActivity(browserIntent);
            }
        });
    }
    /* Initialize share button */
    shareBodyButton.setOnClickListener(new View.OnClickListener() {

        String shareUrl = ShareURLMaker.getBodyURL(body);

        @Override
        public void onClick(View view) {
            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("text/plain");
            i.putExtra(Intent.EXTRA_SUBJECT, "Sharing URL");
            i.putExtra(Intent.EXTRA_TEXT, shareUrl);
            fragment.startActivity(Intent.createChooser(i, "Share URL"));
        }
    });
}
Also used : Response(retrofit2.Response) Call(retrofit2.Call) Callback(retrofit2.Callback) Intent(android.content.Intent) TextView(android.widget.TextView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) RetrofitInterface(app.insti.api.RetrofitInterface)

Example 98 with Callback

use of zipkin2.Callback in project IITB-App by wncc.

the class ComplaintDetailsFragment method upVote.

private void upVote(final Venter.Complaint detailedComplaint) {
    RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
    if (!detailedComplaint.isComplaintUpvoted()) {
        retrofitInterface.upVote(Utils.getSessionIDHeader(), cId, 1).enqueue(new Callback<Venter.Complaint>() {

            @Override
            public void onResponse(Call<Venter.Complaint> call, Response<Venter.Complaint> response) {
                if (response.isSuccessful()) {
                    Venter.Complaint complaint = response.body();
                    detailedComplaint.setComplaintUpvoted(true);
                    addVotesToView(complaint);
                }
            }

            @Override
            public void onFailure(Call<Venter.Complaint> call, Throwable t) {
                Log.i(TAG, "failure in up vote: " + t.toString());
            }
        });
    } else if (detailedComplaint.isComplaintUpvoted()) {
        retrofitInterface.upVote(Utils.getSessionIDHeader(), cId, 0).enqueue(new Callback<Venter.Complaint>() {

            @Override
            public void onResponse(Call<Venter.Complaint> call, Response<Venter.Complaint> response) {
                if (response.isSuccessful()) {
                    Venter.Complaint complaint = response.body();
                    detailedComplaint.setComplaintUpvoted(false);
                    addVotesToView(complaint);
                }
            }

            @Override
            public void onFailure(Call<Venter.Complaint> call, Throwable t) {
                Log.i(TAG, "failure in up vote: " + t.toString());
            }
        });
    }
}
Also used : Response(retrofit2.Response) Venter(app.insti.api.model.Venter) Call(retrofit2.Call) OnMapReadyCallback(com.google.android.gms.maps.OnMapReadyCallback) Callback(retrofit2.Callback) RetrofitInterface(app.insti.api.RetrofitInterface)

Example 99 with Callback

use of zipkin2.Callback in project bitcoin-wallet by bitcoin-wallet.

the class ExchangeRatesRepository method maybeRequestExchangeRates.

private void maybeRequestExchangeRates() {
    if (!application.getConfiguration().isEnableExchangeRates())
        return;
    final Stopwatch watch = Stopwatch.createStarted();
    final long now = System.currentTimeMillis();
    final long lastUpdated = this.lastUpdated.get();
    if (lastUpdated != 0 && now - lastUpdated <= UPDATE_FREQ_MS)
        return;
    final CoinGecko coinGecko = new CoinGecko(new Moshi.Builder().build());
    final Request.Builder request = new Request.Builder();
    request.url(coinGecko.url());
    final Headers.Builder headers = new Headers.Builder();
    headers.add("User-Agent", userAgent);
    headers.add("Accept", coinGecko.mediaType().toString());
    request.headers(headers.build());
    final OkHttpClient.Builder httpClientBuilder = Constants.HTTP_CLIENT.newBuilder();
    httpClientBuilder.connectionSpecs(Collections.singletonList(ConnectionSpec.RESTRICTED_TLS));
    final Call call = httpClientBuilder.build().newCall(request.build());
    call.enqueue(new Callback() {

        @Override
        public void onResponse(final Call call, final Response response) throws IOException {
            try {
                if (response.isSuccessful()) {
                    for (final ExchangeRateEntry exchangeRate : coinGecko.parse(response.body().source())) dao.insertOrUpdate(exchangeRate);
                    ExchangeRatesRepository.this.lastUpdated.set(now);
                    watch.stop();
                    log.info("fetched exchange rates from {}, took {}", coinGecko.url(), watch);
                } else {
                    log.warn("http status {} {} when fetching exchange rates from {}", response.code(), response.message(), coinGecko.url());
                }
            } catch (final Exception x) {
                log.warn("problem fetching exchange rates from " + coinGecko.url(), x);
            }
        }

        @Override
        public void onFailure(final Call call, final IOException x) {
            log.warn("problem fetching exchange rates from " + coinGecko.url(), x);
        }
    });
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Headers(okhttp3.Headers) Stopwatch(com.google.common.base.Stopwatch) Request(okhttp3.Request) IOException(java.io.IOException) IOException(java.io.IOException) Response(okhttp3.Response) Callback(okhttp3.Callback)

Example 100 with Callback

use of zipkin2.Callback in project AndLang by wugemu.

the class HttpU method post.

/**
 * 网络请求方法
 *
 * @param context
 * @param url
 * @param params
 * @param tag
 * @param callback
 */
public void post(final Context context, final String url, Map<String, Object> params, Object tag, final HttpCallback callback) {
    if (context == null) {
        callback.onAfter();
        return;
    }
    // token校验参数
    if (params == null) {
        params = new HashMap<String, Object>();
    }
    FormBody.Builder formBuilder = new FormBody.Builder();
    if (params.size() > 0) {
        Iterator iter = params.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            if (entry != null) {
                if (entry.getKey() != null && entry.getValue() != null) {
                    formBuilder.add(entry.getKey().toString(), entry.getValue().toString());
                }
            }
        }
    }
    FormBody formBody = formBuilder.build();
    String cookie = BaseLangApplication.getInstance().getSpUtil().getString(context, COOKIE);
    if (cookie != null && !"".equals(cookie)) {
        try {
            cookie = Des3.decode(cookie);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    Request.Builder requestBuilder = new Request.Builder();
    if (cookie != null) {
        requestBuilder.header(COOKIE, cookie);
    }
    final Request request = requestBuilder.url(url).post(formBody).tag(url).build();
    if (BaseLangUtil.isApkInDebug()) {
        AppCrashHandler crashHandler = AppCrashHandler.getInstance();
        if (crashHandler != null) {
            crashHandler.saveLogInfo2File("post请求报文Host:" + url);
            crashHandler.saveLogInfo2File("post请求报文cookie:" + cookie);
            crashHandler.saveLogInfo2File("post请求报文body:" + params);
        }
    }
    callback.onBefore(request);
    Call call = mOkHttpClient.newCall(request);
    call.enqueue(new Callback() {

        @Override
        public void onFailure(Call call, final IOException e) {
            onMyFailure(callback, request, e);
        }

        @Override
        public void onResponse(Call call, final Response response) throws IOException {
            onMyResponse(response, context, url, callback, request);
        }
    });
}
Also used : Call(okhttp3.Call) FormBody(okhttp3.FormBody) Request(okhttp3.Request) IOException(java.io.IOException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Response(okhttp3.Response) AppCrashHandler(com.example.test.andlang.log.AppCrashHandler) Callback(okhttp3.Callback) Iterator(java.util.Iterator) JSONObject(org.json.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap) MimeTypeMap(android.webkit.MimeTypeMap)

Aggregations

Callback (okhttp3.Callback)173 IOException (java.io.IOException)137 Call (okhttp3.Call)132 Response (okhttp3.Response)132 Request (okhttp3.Request)110 Callback (retrofit2.Callback)42 Call (retrofit2.Call)41 Test (org.junit.Test)39 Response (retrofit2.Response)39 RequestBody (okhttp3.RequestBody)37 OkHttpClient (okhttp3.OkHttpClient)34 File (java.io.File)27 Context (android.content.Context)24 JSONObject (org.json.JSONObject)20 FormBody (okhttp3.FormBody)19 ArrayList (java.util.ArrayList)18 View (android.view.View)16 Intent (android.content.Intent)14 TextView (android.widget.TextView)14 GsonBuilder (com.google.gson.GsonBuilder)14