com.worklight.poc.Pull.java Source code

Java tutorial

Introduction

Here is the source code for com.worklight.poc.Pull.java

Source

/*
 *  Licensed Materials - Property of IBM
 *  5725-G92 (C) Copyright IBM Corp. 2006, 2012. All Rights Reserved.
 *  US Government Users Restricted Rights - Use, duplication or
 *  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */

package com.worklight.poc;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.worklight.wlclient.api.WLClient;
import com.worklight.wlclient.api.WLFailResponse;
import com.worklight.wlclient.api.WLProcedureInvocationData;
import com.worklight.wlclient.api.WLRequestOptions;
import com.worklight.wlclient.api.WLResponse;
import com.worklight.wlclient.api.WLResponseListener;

public class Pull extends Activity implements WLResponseListener {
    private Button buttonConnect = null;
    private Button buttonInvoke = null;
    private static ImageView img;
    private static TextView textView = null;
    private static TextView textViewbottom = null;
    private static Pull _Pull;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        _Pull = this;
        new NetnServerCheck(_Pull).execute();
        img = (ImageView) findViewById(R.id.imageView1);
        buttonConnect = (Button) findViewById(R.id.buttonConnect);
        buttonInvoke = (Button) findViewById(R.id.buttonInvoke);
        textView = (TextView) findViewById(R.id.textView);
        textViewbottom = (TextView) findViewById(R.id.textViewbottom);
        final WLClient client = WLClient.createInstance(this);

        buttonConnect.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                updateTextView("Connecting...");
                Utility.setPrefData("login", "false", _Pull);
                client.connect(new PullConnectListener());
            }
        });
        buttonInvoke.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                getCatalog(_Pull);
            }
        });
    }

    public static void updateTextView(final String str) {
        Runnable run = new Runnable() {
            @Override
            public void run() {
                textView.setText(str);
            }
        };
        _Pull.runOnUiThread(run);
    }

    public static void updateTextViewbottom(final String str) {
        Runnable run = new Runnable() {
            @Override
            public void run() {
                textViewbottom.setText(str);
            }
        };
        _Pull.runOnUiThread(run);
    }

    public static void getCatalog(Context c) {
        if (NetnServerCheck.netResult) {
            updateTextView("Invoking procedure...");
            String adapterName = "SampleHttpAdapter";
            String procedureName = "getCatalog";
            WLProcedureInvocationData invocationData = new WLProcedureInvocationData(adapterName, procedureName);
            Object[] parameters = new Object[] {};
            invocationData.setParameters(parameters);
            WLRequestOptions options = new WLRequestOptions();
            options.setTimeout(30000);
            if (!Utility.getPrefData("login", null, c).toString().equalsIgnoreCase("true")) {
                updateTextView("Please wait...");
                Intent intent = new Intent(c, LoginTest.class);
                c.startActivity(intent);
                updateTextView("Try Again...");
            }
            WLClient client = WLClient.getInstance();
            client.invokeProcedure(invocationData, new Pull(), options);
        }
    }

    public static void updateImageView(final URL url) {
        Runnable run = new Runnable() {
            @Override
            public void run() {
                Bitmap bmp;
                try {
                    bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                    img.setImageBitmap(bmp);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        _Pull.runOnUiThread(run);
    }

    static JSONParser jParser = new JSONParser();
    JSONObject jsonObject = null;
    JSONArray products = null;

    @Override
    public void onFailure(WLFailResponse response) {
        String responseText = response.getResponseText();
        Log.d("InvokeFail", responseText);
        Pull.updateTextView("Failed to Invoke Adapter Procedure\n" + responseText);
    }

    @Override
    public void onSuccess(WLResponse response) {
        JSONObject responseJSON = response.getResponseJSON();
        try {
            products = responseJSON.getJSONArray("products");
            Log.d("products", products.toString());
            Pull.updateTextViewbottom("Total Number of Results are: " + products.length());

            // for (int i=0; i<products.length(); i++) {
            JSONObject actor = products.getJSONObject(0);
            String productTitle = "Title: " + actor.getString("productTitle");
            String webID = "Web ID: " + actor.getString("webID");

            // getting Price
            JSONObject price = actor.getJSONObject("price");
            // Regular Price
            JSONObject regularPrice = price.getJSONObject("regularPrice");
            String maxPrice = "maxPrice: " + regularPrice.getString("maxPrice");
            String minPrice = "minPrice: " + regularPrice.getString("minPrice");
            // Sale Price
            JSONObject salePrice = price.getJSONObject("salePrice");
            String smaxPrice = "maxPrice: " + salePrice.getString("maxPrice");
            String sminPrice = "minPrice: " + salePrice.getString("minPrice");

            // getting Image
            JSONObject image = actor.getJSONObject("image");
            URL url = new URL(image.getString("url"));
            Pull.updateImageView(url);

            Pull.updateTextView(
                    productTitle + "\n" + webID + "\n" + "Regular Price" + "\n\t\t\t" + maxPrice + "\n\t\t\t"
                            + minPrice + "\n" + "Sale Price" + "\n\t\t\t" + smaxPrice + "\n\t\t\t" + sminPrice);
            // }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}