import android.support.annotation.Keep;
+import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
@Keep
public class SimpleFetcher
{
- public static byte[] fetch(String uri) throws IOException
+ public static byte[] fetch(String uri, byte[] data, String contentType) throws IOException
{
URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(10000);
try
{
+ if (contentType != null)
+ {
+ conn.setRequestProperty("Content-Type", contentType);
+ }
+ if (data != null)
+ {
+ conn.setDoOutput(true);
+ conn.setFixedLengthStreamingMode(data.length);
+ OutputStream out = new BufferedOutputStream(conn.getOutputStream());
+ out.write(data);
+ out.close();
+ }
return streamToArray(conn.getInputStream());
}
finally
}
}
- private static byte[] streamToArray(InputStream in)
+ private static byte[] streamToArray(InputStream in) throws IOException
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
{
e.printStackTrace();
}
+ finally
+ {
+ in.close();
+ }
return null;
}
}
* Callback function
*/
fetcher_callback_t cb;
+
+ /**
+ * Data to POST
+ */
+ chunk_t data;
+
+ /**
+ * Type of data to POST
+ */
+ char *request_type;
};
METHOD(fetcher_t, fetch, status_t,
{
JNIEnv *env;
jmethodID method_id;
- jobjectArray jdata;
- jstring juri;
+ jobjectArray jdata = NULL;
+ jstring juri, jct = NULL;
chunk_t data;
status_t status = FAILED;
androidjni_attach_thread(&env);
/* can't use FindClass here as this is not called by the main thread */
method_id = (*env)->GetStaticMethodID(env, android_simple_fetcher_class,
- "fetch", "(Ljava/lang/String;)[B");
+ "fetch", "(Ljava/lang/String;[BLjava/lang/String;)[B");
if (!method_id)
{
goto failed;
{
goto failed;
}
+ if (this->request_type)
+ {
+ jct = (*env)->NewStringUTF(env, this->request_type);
+ if (!jct)
+ {
+ goto failed;
+ }
+ }
+ if (this->data.ptr)
+ {
+ jdata = byte_array_from_chunk(env, this->data);
+ if (!jdata)
+ {
+ goto failed;
+ }
+ }
jdata = (*env)->CallStaticObjectMethod(env, android_simple_fetcher_class,
- method_id, juri);
+ method_id, juri, jdata, jct);
if (!jdata || androidjni_exception_occurred(env))
{
goto failed;
this->cb = va_arg(args, fetcher_callback_t);
break;
}
+ case FETCH_REQUEST_DATA:
+ {
+ this->data = chunk_clone(va_arg(args, chunk_t));
+ break;
+ }
+ case FETCH_REQUEST_TYPE:
+ {
+ this->request_type = strdup(va_arg(args, char*));
+ break;
+ }
default:
supported = FALSE;
break;
METHOD(fetcher_t, destroy, void,
android_fetcher_t *this)
{
+ chunk_clear(&this->data);
+ free(this->request_type);
free(this);
}