This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface VolleyOnEventListener<T> { | |
public void onSuccess(T object); | |
public void onFailure(Exception e); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
private static final String TAG = "MainActivity"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
try { | |
VolleyServerRequest order = new VolleyServerRequest(getApplicationContext(), new OnEventListener() { | |
@Override | |
public void onSuccess(Object object) { | |
Log.d(TAG, "onSuccess: "+object.toString()); | |
} | |
@Override | |
public void onFailure(Exception e) { | |
Log.e(TAG, "onFailure: ", e); | |
} | |
}); | |
}catch (Exception e){ | |
Log.e(TAG, "displayServer: error:",e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class VolleyServerRequest { | |
Context context; | |
private OnEventListener<String> mCallBack; | |
public VolleyServerRequest(Context context, OnEventListener callback){ | |
this.context = context; | |
mCallBack = callback; | |
setPostRequest(); | |
} | |
public void setPostRequest(){ | |
RequestQueue queue = Volley.newRequestQueue(this.context); | |
String url = "your url"; | |
StringRequest postRequest = new StringRequest(Request.Method.POST, url, | |
new Response.Listener<String>() | |
{ | |
@Override | |
public void onResponse(String response) { | |
// response | |
Log.d("Response", response); | |
mCallBack.onSuccess(response); | |
} | |
}, | |
new Response.ErrorListener() | |
{ | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
// error | |
Log.d("Error.Response", error.toString()); | |
mCallBack.onFailure(error); | |
} | |
} | |
) { | |
@Override | |
protected Map<String, String> getParams() | |
{ | |
Map<String, String> params = new HashMap<String, String>(); | |
params.put("Key", "value"); | |
return params; | |
} | |
@Override | |
public Map<String, String> getHeaders(){ | |
HashMap<String, String> params = new HashMap<String, String>(); | |
String creds = String.format("%s:%s","Usernmae","Password"); | |
String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT); | |
params.put("Authorization", auth); | |
return params; | |
} | |
}; | |
queue.add(postRequest); | |
} | |
} |
0 Comments:
Post a Comment