-4

I am trying to display data from the website in my ListView but it is not showing data. I think that there is an error in my `AsyncTask,

I want to view only two text from the json file I am getting.

help me learn my mistake. I tried using ipadress and domainname nothing works and it is not an exception

here is my code

package com.itdc.shashank.avdeal1;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;


public class itemView extends Activity {
    ArrayList<item> list1;
    actorAdapter adp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_view);
        list1=new ArrayList<item>();
        Log.d("hello", "notworking");
        new JSONAsyncTask().execute("http://avdeal.in/get_all_products.php");


    }

    public class JSONAsyncTask extends AsyncTask<String,Void, Boolean>{
        ProgressDialog pd;
        protected void OnPreexecute(){

            super.onPreExecute();
            pd=new ProgressDialog(itemView.this);
            pd.setMessage("Please Wait");
            pd.setTitle("connecting");
            pd.show();
            pd.setCancelable(false);
        }
        @Override
        protected Boolean doInBackground(String... urls) {
            try {
                HttpGet httppost=new HttpGet(urls[0]);
                HttpClient httpClient=new DefaultHttpClient();
                HttpResponse response=httpClient.execute(httppost);

                int status=response.getStatusLine().getStatusCode();
                if (status>0)
                {
                    HttpEntity entity=response.getEntity();
                    String data= EntityUtils.toString(entity);

                    JSONObject jsono=new JSONObject(data);
                    JSONArray jsonArray=jsono.getJSONArray("item");
                for (int i=0;i<jsonArray.length();i++)
                {
                    JSONObject object=jsonArray.getJSONObject(i);
                    item Items=new item(object.getString("product_title"),object.getString("product_price"));
                   // item.setName(object.getString("product_title"));
                  //  item.setPrice(object.getString("product_price"));
                    list1.add(Items);

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

            return true;
        }

        protected void onPostExecute(boolean result){
            pd.dismiss();

            adp.notifyDataSetChanged();
            if (result==false)
                Toast.makeText(getApplicationContext(), "Unable to fetch data", Toast.LENGTH_SHORT).show();
            ListView lv= (ListView) findViewById(R.id.list);
            adp=new actorAdapter(getApplicationContext(),R.layout.listview, list1);
            lv.setAdapter(adp);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        return true;
    }

   }

THIS is the only logcat I get when I use webaddress but the website is working

07-23 20:37:09.008  13927-14195/com.itdc.shashank.avdeal1 I/System.out﹕ [CDS][DNS]Unable to resolve host "www.avdeal.in": No address associated with hostname
07-23 20:37:09.010  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ java.net.UnknownHostException: Unable to resolve host "www.avdeal.in": No address associated with hostname
07-23 20:37:09.019  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:439)
07-23 20:37:09.019  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-23 20:37:09.020  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-23 20:37:09.020  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-23 20:37:09.020  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-23 20:37:09.020  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-23 20:37:09.021  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
07-23 20:37:09.021  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-23 20:37:09.022  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-23 20:37:09.022  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-23 20:37:09.022  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at com.itdc.shashank.avdeal1.itemView$JSONAsyncTask.doInBackground(itemView.java:57)
07-23 20:37:09.023  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at com.itdc.shashank.avdeal1.itemView$JSONAsyncTask.doInBackground(itemView.java:41)
07-23 20:37:09.024  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-23 20:37:09.024  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-23 20:37:09.025  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-23 20:37:09.025  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-23 20:37:09.026  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-23 20:37:09.026  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at java.lang.Thread.run(Thread.java:838)
07-23 20:37:09.027  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
07-23 20:37:09.029  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at libcore.io.Posix.getaddrinfo(Native Method)
07-23 20:37:09.029  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:59)
07-23 20:37:09.030  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
07-23 20:37:09.031  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ ... 17 more
07-23 20:37:09.031  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ Caused by: libcore.io.ErrnoException: getaddrinfo failed: ETIMEDOUT (Connection timed out)
07-23 20:37:09.033  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ ... 20 more
Amsheer
  • 7,046
  • 8
  • 47
  • 81
  • Are you getting exception ? – Arpit B Parekh Jul 23 '15 at 14:21
  • http://108.170.10.163/get_all_products.php. Did you try this in browser? – Amsheer Jul 23 '15 at 14:22
  • could not open above page . – Arpit B Parekh Jul 23 '15 at 14:22
  • 2
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Jul 23 '15 at 14:23
  • First, your class should start with an uppercase letter. And your itemView that is actually an Activity, maybe you should call it ItemActivity – andrei Jul 23 '15 at 14:24
  • @Selvin How did you say it is a NullPointerException? – Amsheer Jul 23 '15 at 14:25
  • no logcat is cleal at first i got error that host cant be resolved so i used ipadress instead and solved that error – Shashank Shekhar Jul 23 '15 at 14:25
  • hehe, because I'm a real programmer .... this code will end with NPE in this line `adp.notifyDataSetChanged();` – Selvin Jul 23 '15 at 14:26
  • Wow great i just missed it. @Selvin you are great – Amsheer Jul 23 '15 at 14:27
  • ouch, my eyes ! Why not CamelCase ? Using ArrayList instead of List also looks kina wierd to me. – 2Dee Jul 23 '15 at 14:37
  • i am going to use all of the data from that website so i need to use arraylist... this is the link avdeal.in/get_all_products.php – Shashank Shekhar Jul 23 '15 at 14:42
  • Check this [tutorial](http://androhub.com/android-json-parser/) how to fetch data from server and display over listview. Just go through it. – Surender Kumar Jul 23 '15 at 14:42
  • the tutorial is good but it uses different technique i want to know whats wrong in this one please help me out. – Shashank Shekhar Jul 23 '15 at 14:51
  • Do you understand the difference between coding against the interface VS coding against the implementation ? If you're using ArrayList, there is a 99.9% chance you could declare your variable as List instead... Cleaning up the code to use naming conventions and avoiding weird stuff like naming an HttpGet variable by "httppost" (it's almost code trolling) would help you and help us read the code with more ease, and spot mistakes faster. Finally, since the url is returning a 404, I don't see how this could work, anyway. – 2Dee Jul 23 '15 at 14:59
  • i have edited the code and have put the domainname – Shashank Shekhar Jul 23 '15 at 15:07
  • https://www.youtube.com/watch?v=W8_Kfjo3VjU the website is down – petey Jul 23 '15 at 17:34

1 Answers1

0

I guess the issue is related with your url http://108.170.10.163/get_all_products.php. Check this. This url showing the 404(Page not found) error.

Edit:

As per the Comments from @Selvin you just get an NUllPointerException too.

You need to add this adp.notifyDataSetChanged() after you create the adapter. Which means you need to do like this.

 adp=new actorAdapter(getApplicationContext(),R.layout.listview, list1);
 adp.notifyDataSetChanged();
Amsheer
  • 7,046
  • 8
  • 47
  • 81
  • these are allready added... and i used the webaddress too it is not working its not nullpointer exception – Shashank Shekhar Jul 23 '15 at 14:33
  • since the adapter is populated when passing the 'list1' reference to the adapter's constructor, I think calling 'notifyDataSetChanged on it should have no effect. – 2Dee Jul 23 '15 at 14:34