json_parsing_simplified_coding.net/demos/marvel_uel

Step 1:Manifest ma internet ni permission
Step 2:build graddle(module:app)

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.squareup.picasso:picasso:2.71828'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    implementation 'com.android.support:recyclerview-v7:26.1.0'
}
Step 3:Activiry_main_xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="demoproject.aalap.com.jay.MainActivity">
<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/lv"></ListView>

</LinearLayout>

Step 4:simple_xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv"
        android:background="@mipmap/ic_launcher"
        android:layout_width="100dp"
        android:layout_height="100dp" />

    <TextView
        android:id="@+id/tv"
        android:textColor="#000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Step 5:Hero //hero java ma file bnavi hero itle pojjo jema value gete and set tay

public class Hero {

      private String name;
        private String realname;
        private String team;
        private String firstappearance;
        private String createdby;
        private String publisher;
        private String imageurl;
        private String bio;
//aatlu lkheya pache right click kari ne getter and setter karvu

Step 6:jsonAdapter  //jsonadapter name ni java ma file bnaave
class jsonAdapter extends BaseAdapter {
    Context context;
    ArrayList<Hero> arryList=new ArrayList<>();


    public jsonAdapter(Context context, ArrayList<Hero> arryList) {
        this.context = context;
        this.arryList = arryList;
    }

    @Override
    public int getCount() {
        return arryList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater inflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View v=inflater.inflate(R.layout.simple,null);

        ImageView imageView;
        TextView textView;

        imageView=v.findViewById(R.id.iv);
        textView=v.findViewById(R.id.tv);

        Hero h=arryList.get(i);
        textView.setText(h.getName() +"\n"+ h.getRealname() +"\n"+h.getTeam() +"\n"+ h.getFirstappearance() +"\n"+h.getCreatedby() +"\n"+h.getPublisher() +"\n"+h.getImageurl() +"\n"+h.getBio());
        //Picasso.get().load(h.getImageurl()).into(imageView);
        return v;
    }
}

Step 7:json class
public class jsonclass {
    String result;


    public String processurl(String s)
    {
        try {
            URL url=new URL(s);

            HttpURLConnection conn= (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");

            int code=conn.getResponseCode();

            if (code==200)
            {
                InputStream in=new BufferedInputStream(conn.getInputStream());
                BufferedReader br=new BufferedReader(new InputStreamReader(in));

                StringBuilder sb=new StringBuilder();
                String line="";
                while ((line=br.readLine())!=null)
                {
                    sb.append(line+"\n");
                }
                result=sb.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();

        }
        return  result;
    }
}

Step 8:MainActivity.java
public class MainActivity extends AppCompatActivity {

    ListView lv;
    String URL_GATE_DATA="https://simplifiedcoding.net/demos/marvel",result;
    ArrayList<Hero> arrayList=new ArrayList<>();

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

        lv=findViewById(R.id.lv);

        select sel=new select();//innter class take
        sel.execute();
    }

    private class select extends AsyncTask {
        ProgressDialog pg;
        jsonclass js;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pg=new ProgressDialog(MainActivity.this);
            pg.setTitle("Fetching....");
            pg.setMessage("wait");
            pg.setCancelable(true);
            pg.setIndeterminate(false);
            pg.show();
        }

        @Override
        protected Object doInBackground(Object[] objects) {


            js=new jsonclass();
            result=js.processurl(URL_GATE_DATA);

            try {
                JSONArray ja = new JSONArray(result);


                for (int i = 0; i < ja.length(); i++) {

                    JSONObject c = ja.getJSONObject(i);

                    Hero h=new Hero();



                    h.setName(c.getString("name"));
                    h.setRealname(c.getString("realname"));
                    h.setTeam(c.getString("team"));
                    h.setFirstappearance(c.getString("firstappearance"));
                    h.setCreatedby(c.getString("createdby"));
                    h.setPublisher(c.getString("name"));
                    h.setImageurl(c.getString("imageurl"));
                    h.setBio(c.getString("bio"));



                    arrayList.add(h);

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

            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);

            if(pg.isShowing()){
                pg.dismiss();
            }
            jsonAdapter jsonadapter=new jsonAdapter(MainActivity.this,arrayList);
            lv.setAdapter(jsonadapter);
        }
    }
}

Comments

Popular posts from this blog

Seaborn

profile fragment firebase ie image and information vadu page update tay firebase ma

Payment ideal