Skip to main content

Android Volley

Key Components  

  • RequestQueue: A Queue containing the Network/HTTP Requests that needs to be made.
  • Request: A Base Class which contains Network related information like HTTP Methods.
  • StringRequest: HTTP Request where the response is parsed a String. View Source
  • JsonObjectRequest: HTTP Request where the response is JSONObject. View Source

private static final String URL = "http://httpbin.org/get?param1=hello";private static final String TAG = MainActivity.class.getSimpleName();@BindView(R.id.dataView) TextView dataView;@Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_volly_sample);    ButterKnife.bind(this);    RequestQueue requestQueue  = Volley.newRequestQueue(this);    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, URL, null,            new Response.Listener<JSONObject>() {

                @Override                public void onResponse(JSONObject response) {
                    Log.d(TAG, "onResponse: " + response.toString());                    dataView.setText(response.toString());                }
            },            new Response.ErrorListener() {
                @Override                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, "onErrorResponse: " + error.getMessage());                    dataView.setText(error.toString());
                }
            });    requestQueue.add(jsonObjectRequest);}





Comments

Popular posts from this blog

Android Room + Card View + RecyclerVIew

@Entity public class User { @PrimaryKey(autoGenerate = true) private Long userid; private String username; private String usercontact; private String useremail; } @Dao public interface UserDao { @Insert Long SaveUser(User user); @Update int UpdateUser(User user); @Query("SELECT * FROM USER WHERE USER.userid = :id") User FindUserById(Long id); @Query("SELECT * FROM USER") List<User> FindAll(); @Query("DELETE FROM USER WHERE USER.userid = :id") void DeleteUserById(Long id); } @Database(entities = {User.class}, version = 1, exportSchema = false) public abstract class UserDatabase extends RoomDatabase{ public abstract UserDao userDao(); } public class UserAdapter extends RecyclerView.Adapter<UserAdapter.MyViewHolder> List<User> userList; private Context mContext; https://github.com/gsandaru/AndroidRoomExample onCreateViewHolder---------------...

Retrofit

Dependencies compile 'com.squareup.retrofit2:retrofit:2.4.0' compile 'com.squareup.retrofit2:converter-gson:2.4.0'     configurations.all {         resolutionStrategy.force 'com.android.support:support-annotations:23.1.0'     } 1. Create Model Class + getters Setters 2. Create Layout 3. Create RetrofitClient public class RetrofitClient {     private static Retrofit retrofitClient = null;     public static Retrofit getRetrofitClient(String baseurl) {         if(retrofitClient == null){             retrofitClient = new Retrofit.Builder()                     .baseUrl(baseurl)                     .addConverterFactory(GsonConverterFactory.create()).build();         }         return retrofitClient;     } } 4...