Skip to content

Instantly share code, notes, and snippets.

@darinwilson
Last active March 21, 2020 22:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darinwilson/25a7f05d0b1a440e9be8 to your computer and use it in GitHub Desktop.
Save darinwilson/25a7f05d0b1a440e9be8 to your computer and use it in GitHub Desktop.
Java extension to get a POST request working with Volley in RubyMotion for Android
/*
* This assumes that you have a file called request.rb in the same directory, and that
* the file contains a subclass of Volley request object
*
* The code in this .java file gets added to the Java wrapper around our Ruby class at
* compile time. This contains a few shims to work around cases where RubyMotion is
* currently not generating correctly typed versions of these methods.
*
* http://hipbyte.myjetbrains.com/youtrack/issue/RM-724
*
*/
// calls for the Ruby wrapper
private java.util.Map<java.lang.String,java.lang.String> mParams;
private java.util.Map<java.lang.String,java.lang.String> mHeaders;
public void setHeaders(java.util.Map<com.rubymotion.String,com.rubymotion.String> headers) {
mHeaders = convertMap(headers);
}
public void setParams(java.util.Map<com.rubymotion.String,com.rubymotion.String> params) {
mParams = convertMap(params);
}
/*
* this converts Ruby Hash objects (keyed with com.rubymotion.String objects) to HashMap
* objects that use java.lang.String objects as keys and values, which are needed for Volley's
* framework calls
*/
private java.util.Map<java.lang.String,java.lang.String> convertMap(
java.util.Map<com.rubymotion.String, com.rubymotion.String> map) {
java.util.HashMap<java.lang.String,java.lang.String> newMap =
new java.util.HashMap<java.lang.String, java.lang.String>();
for (java.util.Map.Entry<com.rubymotion.String,com.rubymotion.String> entry : map.entrySet()) {
java.lang.Object value = entry.getValue();
if (value != null) {
newMap.put(entry.getKey().toString(), value.toString());
}
}
return newMap;
}
// Volley framework calls
@Override
protected java.util.Map<java.lang.String,java.lang.String> getParams() throws com.android.volley.AuthFailureError {
return mParams;
}
@Override
public java.util.Map<java.lang.String,java.lang.String> getHeaders() throws com.android.volley.AuthFailureError {
return mHeaders;
}
~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment