Andriod httpurlconnection with WooCommerce OAuth1 API

Please help me.. trying to connect woocommerce website through Android Native app using OAuth1 API.
I am trying this code..
In MainActivity
WE ARE NOT GETTING INPUTSTREAM AND MY APP TERMINATE..
In this code if i use conn.getInputStream(), then my app is directry terminated and if i comment that line then app is not terminated but i do not get any responce from url . Please help

    RequestPackage p = new RequestPackage();
    p.setMethod("GET");
    p.setParm("oauth_signature_method",method);
    p.setUri(BASE_URL);
    p.setParm("oauth_nonce",oauth_nonce);
    p.setParm("oauth_consumer_key",CONSUMER_KEY);
    p.setParm("oauth_version","1.0");
    p.setParm("oauth_timestamp",oauth_timestamp);
    p.setParm("oauth_signature", signature);

    HttpHandler sh = new HttpHandler();
    String jsonStr = sh.makeServiceCall(p);


public class HttpHandler {

    public HttpHandler() {
    }

    public String makeServiceCall(RequestPackage p) {

        String reqUrl = p.getUri();
        if (p.getMethod()=="GET"){
            reqUrl+="?"+p.getEncodedParams();
        }
        String response = null;
        try {
            URL url = new URL(reqUrl);
            Log.e(TAG, "Connection url is : "+ String.valueOf(url));
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setDoOutput(true);

            InputStream in = conn.getInputStream();
            response = convertStreamToString(in);
            Log.e(TAG, "Connection Completed : "+ String.valueOf(conn));

        } catch (MalformedURLException e) {
            Log.e(TAG, "MalformedURLException: " + e.getMessage());
        } catch (ProtocolException e) {
            Log.e(TAG, "ProtocolException: " + e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, "IOException  343: " + e.getMessage());
        }
        return response;
    }

    private String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line).append('n');
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return sb.toString();
    }
}

This is my RequestPackge class

public class RequestPackage {
    private String uri;
    private  String method = "GET";
    private Map<String,String> params = new HashMap<>();


    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public void setParams(Map<String, String> params) {
        this.params = params;
    }
    public void setParm(String key, String value){
        params.put(key,value);
    }
    public String getEncodedParams(){
        StringBuilder sb = new StringBuilder();
        for (String key : params.keySet()){
            String value = null;
            Log.d("Kyesss",key);
            try {
                value = URLEncoder.encode(params.get(key),"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            if (sb.length()>0){
                sb.append("&");
            }
            sb.append(key + "=" + value);
        }
        return sb.toString();
    }
}

Read more here: Andriod httpurlconnection with WooCommerce OAuth1 API

Leave a Reply

Your email address will not be published. Required fields are marked *