interface example

step 1:-
create interface class

package com.example.myapp

import org.json.JSONObject;


public interface ApiResponse {
    void onApiRespond(JSONObject Resposne);
}




step 2:- 
create async task


import android.os.AsyncTask;
import android.util.Log;

import com.example.digital.CustomSSLSocketFactory;
import com.example.digital.Model.Debugger;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.security.KeyStore;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;


public class ApiAsyncTask extends AsyncTask<String, Void, String> {
    private JSONObject jsonObject = new JSONObject();
    private ApiResponse apiResponse;

    public ApiAsyncTask(JSONObject jsonObject) {
        this.jsonObject = jsonObject;
    }

    public void setApiResponse(ApiResponse apiResponse) {
        this.apiResponse = apiResponse;
    }

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected String doInBackground(String... urls) {
        String responceStr = "";
        try {
            HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

            DefaultHttpClient client = new DefaultHttpClient();
            SchemeRegistry registry = new SchemeRegistry();
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);
            SSLSocketFactory socketFactory = new CustomSSLSocketFactory(trustStore);
            socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
            registry.register(new Scheme("https", socketFactory, 443));
            SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
            DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
            // Set verifier
            HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);


            HttpPost httppost = new HttpPost(urls[0]);

            httppost.addHeader("Content-Type", "application/json");

            httppost.setEntity(new StringEntity(jsonObject.toString()));

            HttpResponse response = httpClient.execute(httppost);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    responceStr = EntityUtils.toString(entity);
                }
            }
        } catch (Exception e) {
            Debugger.logD("api error", e.toString() + " ," + urls[0]);
            responceStr = "";
        }
        Log.i("Quotaion Submit", responceStr);
        return responceStr;
    }

    @Override
    protected void onPostExecute(String result) {
        JSONObject jsonObject = null;
        if (!result.equalsIgnoreCase(""))
            try {
                jsonObject = new JSONObject(result);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        apiResponse.onApiRespond(jsonObject);
    }
}



step 3:-   how to call

 ApiAsyncTask apiAsyncTask = new ApiAsyncTask(obj);
                apiAsyncTask.execute("http......your service url");
                apiAsyncTask.setApiResponse(new ApiResponse() {
                    @Override
                    public void onApiRespond(JSONObject json) {
                });




Comments

Popular posts from this blog

Radio Button style

android wifi debugging without root android studio