package com.andela.webservice;

import android.app.ListActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.andela.webservice.model.Flower;
import com.andela.webservice.parser.FlowerJsonParser;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ListActivity {

    public static final String PHOTOS_BASE_URL =
            "http://services.hanselandpetal.com/photos/";

    TextView output;
    ProgressBar pb;

    List<Flower> flowerList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pb = (ProgressBar) findViewById(R.id.progressBar1);
        pb.setVisibility(View.INVISIBLE);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_do_task) {
            if (isOnline()) {
                requestData("http://services.hanselandpetal.com/secure/flowers.json");
            } else {
                Toast.makeText(this, "Network isn't available", Toast.LENGTH_LONG).show();
            }
        }
        return false;
    }

    private void requestData(String uri) {
        // To work with volley, we construct a request object
        //There are different types of request object

        StringRequest request = new StringRequest(uri,

                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String respose) {
                        flowerList = FlowerJsonParser.parseFeed(respose);
                        updateDisplay();
                    }
                },

                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError ex) {
                        Toast.makeText(MainActivity.this, ex.getMessage(), Toast.LENGTH_LONG);
                    }
                }
        );

        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(request);
    }

    protected void updateDisplay() {
        //Use FlowerAdapter to display data
        FlowerAdapter adapter = new FlowerAdapter(this, R.layout.item_flower, flowerList);
        setListAdapter(adapter);
    }

    protected boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        } else {
            return false;
        }
    }

    //volley will handle this

/*
    private class MyTask extends AsyncTask<String, String, List<Flower>> {

        @Override
        protected void onPreExecute() {
            if (tasks.size() == 0) {
                pb.setVisibility(View.VISIBLE);
            }
            tasks.add(this);
        }

        @Override
        protected List<Flower> doInBackground(String... params) {

            String content = HttpManager.getData(params[0], "feeduser", "feedpassword");
            flowerList = FlowerJsonParser.parseFeed(content);

            return flowerList;
        }

        @Override
        protected void onPostExecute(List<Flower> result) {

            tasks.remove(this);
            if (tasks.size() == 0) {
                pb.setVisibility(View.INVISIBLE);
            }

            if (result == null) {
                Toast.makeText(MainActivity.this, "Web service not available", Toast.LENGTH_LONG).show();
                return;
            }

            flowerList = result;
            updateDisplay();

        }

    }
*/

}
@@ -217,16 +217,41 @@
 Bundle;%0A
+import android.util.Log;%0A
 import a
@@ -268,16 +268,16 @@
 w.Menu;%0A
-
 import a
@@ -729,36 +729,8 @@
 y;%0A%0A
-import java.util.ArrayList;%0A
 impo
@@ -1626,14 +1626,13 @@
 com/
-secure
+feeds
 /flo
@@ -2559,16 +2559,23 @@
 TH_LONG)
+.show()
 ;%0A      
@@ -3271,1074 +3271,12 @@
   %7D%0A
+
     %7D%0A%0A
-    //volley will handle this%0A%0A/*%0A    private class MyTask extends AsyncTask%3CString, String, List%3CFlower%3E%3E %7B%0A%0A        @Override%0A        protected void onPreExecute() %7B%0A            if (tasks.size() == 0) %7B%0A                pb.setVisibility(View.VISIBLE);%0A            %7D%0A            tasks.add(this);%0A        %7D%0A%0A        @Override%0A        protected List%3CFlower%3E doInBackground(String... params) %7B%0A%0A            String content = HttpManager.getData(params%5B0%5D, %22feeduser%22, %22feedpassword%22);%0A            flowerList = FlowerJsonParser.parseFeed(content);%0A%0A            return flowerList;%0A        %7D%0A%0A        @Override%0A        protected void onPostExecute(List%3CFlower%3E result) %7B%0A%0A            tasks.remove(this);%0A            if (tasks.size() == 0) %7B%0A                pb.setVisibility(View.INVISIBLE);%0A            %7D%0A%0A            if (result == null) %7B%0A                Toast.makeText(MainActivity.this, %22Web service not available%22, Toast.LENGTH_LONG).show();%0A                return;%0A            %7D%0A%0A            flowerList = result;%0A            updateDisplay();%0A%0A        %7D%0A%0A    %7D%0A*/%0A%0A
 %7D

Fix error with the url link