Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How can used volley library & volley methods in android app

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 271
    Comment on it

    In the below example I have created simple demo app used with volley library. First I have added volley support library in build.gradle file, Then I have added Button and Listview in activity_main.xml. I have created a new list_view.xml layout and here I have added three TextView : get id, name, email.  
    In next step I have created CustomList and ParseJSON Class. In CustomList I have used ArrayAdapter and In ParseJSON class I have created keys. In mainActivity I have used sendRequest() method to get value, in MainActivity I have also used ErrorListener()method it's work when server get not respones like netork issue(server error). You can see below program it will clearly describe you to use volley library and volley methods.

    Step(1)-Added first volley support  library in   build.gradle file

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:22.2.0'
        compile 'com.mcxiaoke.volley:library-aar:1.0.0'
    }

    Step(2)-activity_main.xml-

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Get Data"
            android:id="@+id/buttonGet" />
    
        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/listView" />
    
    
    </LinearLayout>
    

    Step(3)-Created a new  list_view.xml layout-

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textViewId" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textViewName" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textViewEmail" />
    
    </LinearLayout>

    Step(4)-Created CustomList class-

    public class CustomList extends ArrayAdapter<String> {
        private String[] ids;
        private String[] names;
        private String[] emails;
        private Activity context;
    
        public CustomList(Activity context, String[] ids, String[] names, String[] emails) {
            super(context, R.layout.list_view, ids);
            this.context = context;
            this.ids = ids;
            this.names = names;
            this.emails = emails;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = context.getLayoutInflater();
            View listViewItem = inflater.inflate(R.layout.list_view, null, true);
            TextView textViewId = (TextView) listViewItem.findViewById(R.id.textViewId);
            TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
            TextView textViewEmail = (TextView) listViewItem.findViewById(R.id.textViewEmail);
    
            textViewId.setText(ids[position]);
            textViewName.setText(names[position]);
            textViewEmail.setText(emails[position]);
    
            return listViewItem;
        }
    }

    Step(5)-Created ParseJSON class-

    public class ParseJSON {
        public static String[] ids;
        public static String[] names;
        public static String[] emails;
    
        public static final String JSON_ARRAY = "result";
        public static final String KEY_ID = "id";
        public static final String KEY_NAME = "name";
        public static final String KEY_EMAIL = "email";
    
        private JSONArray users = null;
    
        private String json;
    
        public ParseJSON(String json){
            this.json = json;
        }
    
        protected void parseJSON(){
            JSONObject jsonObject=null;
            try {
                jsonObject = new JSONObject(json);
                users = jsonObject.getJSONArray(JSON_ARRAY);
    
                ids = new String[users.length()];
                names = new String[users.length()];
                emails = new String[users.length()];
    
                for(int i=0;i<users.length();i++){
                    JSONObject jo = users.getJSONObject(i);
                    ids[i] = jo.getString(KEY_ID);
                    names[i] = jo.getString(KEY_NAME);
                    emails[i] = jo.getString(KEY_EMAIL);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    Step(6)-MainActivity-

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        public static final String JSON_URL = "http://simplifiedcoding.16mb.com/UserRegistration/json.php";
    
        private Button buttonGet;
    
        private ListView listView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            buttonGet = (Button) findViewById(R.id.buttonGet);
            buttonGet.setOnClickListener(this);
            listView = (ListView) findViewById(R.id.listView);
        }
    
        private void sendRequest(){
    
            StringRequest stringRequest = new StringRequest(JSON_URL,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            showJSON(response);
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(MainActivity.this,error.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    });
    
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
        }
    
        private void showJSON(String json){
            ParseJSON pj = new ParseJSON(json);
            pj.parseJSON();
            CustomList cl = new CustomList(this, ParseJSON.ids,ParseJSON.names,ParseJSON.emails);
            listView.setAdapter(cl);
        }
    
        @Override
        public void onClick(View v) {
            sendRequest();
        }
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: