0

MyActivity.java this is the first file who will run

    package com.example.RadarOperationMonitoringSystem;

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.ViewPager;

    public class MyActivity extends FragmentActivity {

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

            ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
            pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        }
    private class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int pos) {
            switch(pos) {

                case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
                case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
                case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
                default: return ThirdFragment.newInstance("ThirdFragment, Default");
            }
        }

        @Override
        public int getCount() {
            return 3;
        }
    }
}

Here's the Second Fragment where I put the list view form database SecondFragment.java

    package com.example.RadarOperationMonitoringSystem;

    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.support.v4.app.ListFragment;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ListAdapter;
    import android.widget.SimpleAdapter;
    import org.json.JSONException;
    import org.json.JSONObject;

    import java.util.ArrayList;
    import java.util.HashMap;

    public class SecondFragment extends ListFragment {

    // URL to get contacts JSON
    public static final String url = "http://10.0.2.2/radaroperations/tagaytayEnergyReadings.php";

    // JSON Node names
    public static final String TAG_SITENAME1 = "siteName1";
    private static final String TAG_FREQUENCY1 = "Frequency1";
    private static final String TAG_ACCURRENT1 = "AC_Voltage1";
    private static final String TAG_ACVOLTAGE1 = "AC_Current1";

    private static final String TAG_SITENAME2 = "siteName2";
    private static final String TAG_FREQUENCY2 = "Frequency2";
    private static final String TAG_ACCURRENT2 = "AC_Voltage2";
    private static final String TAG_ACVOLTAGE2 = "AC_Current2";

    private static final String TAG_SITENAME3 = "siteName3";
    private static final String TAG_FREQUENCY3 = "Frequency3";
    private static final String TAG_ACCURRENT3 = "AC_Voltage3";
    private static final String TAG_ACVOLTAGE3 = "AC_Current3";

    private static final String TAG_SITENAME4 = "siteName4";
    private static final String TAG_FREQUENCY4 = "Frequency4";
    private static final String TAG_ACCURRENT4 = "AC_Voltage4";
    private static final String TAG_ACVOLTAGE4 = "AC_Current4";

    private static final String TAG_SITENAME5 = "siteName5";
    private static final String TAG_FREQUENCY5 = "Frequency5";
    private static final String TAG_ACCURRENT5 = "AC_Voltage5";
    private static final String TAG_ACVOLTAGE5 = "AC_Current5";

    private static final String TAG_SITENAME6 = "siteName6";
    private static final String TAG_FREQUENCY6 = "Frequency6";
    private static final String TAG_ACCURRENT6 = "AC_Voltage6";
    private static final String TAG_ACVOLTAGE6 = "AC_Current6";

    private static final String TAG_SITENAME7 = "siteName7";
    private static final String TAG_FREQUENCY7 = "Frequency7";
    private static final String TAG_ACCURRENT7 = "AC_Voltage7";
    private static final String TAG_ACVOLTAGE7 = "AC_Current7";

    // contacts JSONArray
    JSONObject c = null;

    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.second_frag, container, false);

        new GetContacts().execute();
        return v;
    }


    public class GetContacts extends AsyncTask<String, String, JSONObject> {

        @Override
            protected JSONObject doInBackground(String... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);

            try {
                JSONObject c = new JSONObject(jsonStr);

                // Getting JSON Array node
                String sitea = c.getString(TAG_SITENAME1);
                String freq1 = c.getString(TAG_FREQUENCY1);
                String curr1 = c.getString(TAG_ACCURRENT1);
                String volts1 = c.getString(TAG_ACVOLTAGE1);

                String site2 = c.getString(TAG_SITENAME2);
                String freq2 = c.getString(TAG_FREQUENCY2);
                String curr2 = c.getString(TAG_ACCURRENT2);
                String volts2 = c.getString(TAG_ACVOLTAGE2);

                String site3 = c.getString(TAG_SITENAME3);
                String freq3 = c.getString(TAG_FREQUENCY3);
                String curr3 = c.getString(TAG_ACCURRENT3);
                String volts3 = c.getString(TAG_ACVOLTAGE3);

                String site4 = c.getString(TAG_SITENAME4);
                String freq4 = c.getString(TAG_FREQUENCY4);
                String curr4 = c.getString(TAG_ACCURRENT4);
                String volts4 = c.getString(TAG_ACVOLTAGE4);

                String site5 = c.getString(TAG_SITENAME5);
                String freq5 = c.getString(TAG_FREQUENCY5);
                String curr5 = c.getString(TAG_ACCURRENT5);
                String volts5 = c.getString(TAG_ACVOLTAGE5);

                String site6 = c.getString(TAG_SITENAME6);
                String freq6 = c.getString(TAG_FREQUENCY6);
                String curr6 = c.getString(TAG_ACCURRENT6);
                String volts6 = c.getString(TAG_ACVOLTAGE6);

                String site7 = c.getString(TAG_SITENAME7);
                String freq7 = c.getString(TAG_FREQUENCY7);
                String curr7 = c.getString(TAG_ACCURRENT7);
                String volts7 = c.getString(TAG_ACVOLTAGE7);
                // tmp hashmap for single contact
                HashMap<String, String> contact = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                contact.put(TAG_SITENAME1, sitea);
                contact.put(TAG_FREQUENCY1, freq1);
                contact.put(TAG_ACCURRENT1, curr1);
                contact.put(TAG_ACVOLTAGE1, volts1);

                contact.put(TAG_SITENAME2, site2);
                contact.put(TAG_FREQUENCY2, freq2);
                contact.put(TAG_ACCURRENT2, curr2);
                contact.put(TAG_ACVOLTAGE2, volts2);

                contact.put(TAG_SITENAME3, site3);
                contact.put(TAG_FREQUENCY3, freq3);
                contact.put(TAG_ACCURRENT3, curr3);
                contact.put(TAG_ACVOLTAGE3, volts3);

                contact.put(TAG_SITENAME4, site4);
                contact.put(TAG_FREQUENCY4, freq4);
                contact.put(TAG_ACCURRENT4, curr4);
                contact.put(TAG_ACVOLTAGE4, volts4);

                contact.put(TAG_SITENAME5, site5);
                contact.put(TAG_FREQUENCY5, freq5);
                contact.put(TAG_ACCURRENT5, curr5);
                contact.put(TAG_ACVOLTAGE5, volts5);

                contact.put(TAG_SITENAME6, site6);
                contact.put(TAG_FREQUENCY6, freq6);
                contact.put(TAG_ACCURRENT6, curr6);
                contact.put(TAG_ACVOLTAGE6, volts6);

                contact.put(TAG_SITENAME7, site7);
                contact.put(TAG_FREQUENCY7, freq7);
                contact.put(TAG_ACCURRENT7, curr7);
                contact.put(TAG_ACVOLTAGE7, volts7);

                // adding contact to contact list
                contactList.add(contact);

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }
        @Override
        public void onPostExecute(JSONObject result) {
            super.onPostExecute(result);

            ListAdapter adapter = new SimpleAdapter(
                    getActivity(), contactList,
                    R.layout.list_item, new String[] { TAG_SITENAME1, TAG_FREQUENCY1, TAG_ACCURRENT1,
                    TAG_ACVOLTAGE1,TAG_SITENAME2, TAG_FREQUENCY2, TAG_ACCURRENT2,
                    TAG_ACVOLTAGE2,TAG_SITENAME3, TAG_FREQUENCY3, TAG_ACCURRENT3,
                    TAG_ACVOLTAGE3, TAG_SITENAME4, TAG_FREQUENCY4, TAG_ACCURRENT4,
                    TAG_ACVOLTAGE4, TAG_SITENAME5, TAG_FREQUENCY5, TAG_ACCURRENT5,
                    TAG_ACVOLTAGE5, TAG_SITENAME6, TAG_FREQUENCY6, TAG_ACCURRENT6,
                    TAG_ACVOLTAGE6, TAG_SITENAME7, TAG_FREQUENCY7, TAG_ACCURRENT7,
                    TAG_ACVOLTAGE7},
                    new int[] { R.id.sitename1,R.id.frequency1,
                            R.id.accurrent1, R.id.acvoltage1, R.id.sitename2, R.id.frequency2,
                            R.id.accurrent2, R.id.acvoltage2, R.id.sitename3, R.id.frequency3,
                            R.id.accurrent3, R.id.acvoltage3, R.id.sitename4, R.id.frequency4,
                            R.id.accurrent4, R.id.acvoltage4, R.id.sitename5, R.id.frequency5,
                            R.id.accurrent5, R.id.acvoltage5, R.id.sitename6, R.id.frequency6,
                            R.id.accurrent6, R.id.acvoltage6, R.id.sitename7, R.id.frequency7,
                            R.id.accurrent7, R.id.acvoltage7});
            // updating listviews
            setListAdapter(adapter);



        }

    }



    public static SecondFragment newInstance(String text) {

        SecondFragment f = new SecondFragment();
        Bundle b = new Bundle();
        b.putString("msg", text);

        f.setArguments(b);

        return f;
    }
}

Here's the ServiceHandler.java code

    package com.example.RadarOperationMonitoringSystem;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

public class ServiceHandler{

    static String response = null;
    public final static int GET = 1;
    public final static int POST = 2;

    public ServiceHandler() {

    }

    /**
     * Making service call
     * @url - url to make request
     * @method - http request method
     * */
    public String makeServiceCall(String url, int method) {
        return this.makeServiceCall(url, method, null);
    }

    /**
     * Making service call
     * @url - url to make request
     * @method - http request method
     * @params - http request params
     * */
    public String makeServiceCall(String url, int method,
            List<NameValuePair> params) {
        try {
            // http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            // Checking http request method type
            if (method == POST) {
                HttpPost httpPost = new HttpPost(url);
                // adding post params
                if (params != null) {
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }

                httpResponse = httpClient.execute(httpPost);

            } else if (method == GET) {
                // appending params to url
                if (params != null) {
                    String paramString = URLEncodedUtils
                            .format(params, "utf-8");
                    url += "?" + paramString;
                }
                HttpGet httpGet = new HttpGet(url);

                httpResponse = httpClient.execute(httpGet);

            }
            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;

    }
}

Here's the logcat when I run it:

 02-10 03:11:18.452    2724-2724/com.example.RadarOperationMonitoringSystem E/Trace﹕ error opening trace file: No such file or directory (2)
02-10 03:11:18.962      786-851/? E/SurfaceFlinger﹕ ro.sf.lcd_density must be defined as a build property
02-10 03:11:20.373      786-843/? E/SurfaceFlinger﹕ ro.sf.lcd_density must be defined as a build property
02-10 03:11:25.942      786-847/? E/SurfaceFlinger﹕ ro.sf.lcd_density must be defined as a build property
02-10 03:11:27.482    2724-2741/com.example.RadarOperationMonitoringSystem E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #4
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
            at java.util.concurrent.FutureTask.run(FutureTask.java:239)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.NullPointerException
            at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
            at org.json.JSONTokener.nextValue(JSONTokener.java:94)
            at org.json.JSONObject.<init>(JSONObject.java:154)
            at org.json.JSONObject.<init>(JSONObject.java:171)
            at com.example.RadarOperationMonitoringSystem.SecondFragment$GetContacts.doInBackground(SecondFragment.java:86)
            at com.example.RadarOperationMonitoringSystem.SecondFragment$GetContacts.doInBackground(SecondFragment.java:73)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:856)
02-10 03:11:27.532      786-852/? E/SurfaceFlinger﹕ ro.sf.lcd_density must be defined as a build property
02-10 03:11:28.102      786-846/? E/SurfaceFlinger﹕ ro.sf.lcd_density must be defined as a build property
02-10 03:11:28.612      786-846/? E/SurfaceFlinger﹕ ro.sf.lcd_density must be defined as a build property

PLEASE HELP ME. I NEED TO FIX THIS APP BADLY! THANK YOU!

Lemuel
  • 31
  • 1
  • 5
  • is `ServiceHandler` class extending Service class? – ρяσѕρєя K Feb 10 '14 at 03:31
  • Not. Do I need it? I don't think I need it because I can already get data from the database using the sample code from other programmers. but the one I'm creating right now is the one not functioning well – Lemuel Feb 10 '14 at 03:33
  • Also share ServiceHandler code – ρяσѕρєя K Feb 10 '14 at 03:37
  • Ok sir. Above is the ServiceHandler code. – Lemuel Feb 10 '14 at 03:39
  • Please see [Android emulator spams logcat with "Service com.android.exchange.ExchangeService has leaked ServiceConnection … that was originally bound here](http://stackoverflow.com/questions/13765122/android-emulator-spams-logcat-with-service-com-android-exchange-exchangeservice) post – ρяσѕρєя K Feb 10 '14 at 03:42
  • Ahmm that doesn't fix the app. Anyway, sir, above I inputted the latest logcat, sorry I didn't notice that there are missing lines on my first post. Hope you can still help me. – Lemuel Feb 10 '14 at 03:49
  • not able to see `Log.d("Response: ", "> " + jsonStr);` line in log. probably `jsonStr` is null due to not getting response from server – ρяσѕρєя K Feb 10 '14 at 03:58
  • I don't think that's the problem, because the app on the sample is actually working. – Lemuel Feb 10 '14 at 04:51
  • i tried to add more logs so that I will no if I can't get any data from the url, and you're right. `3123-3140/com.example.RadarOperationMonitoringSystem E/ServiceHandler﹕ Couldn't get any data from the url` What will I do now? – Lemuel Feb 10 '14 at 05:07

0 Answers0