Explorar el Código

Fix Android compile.

Miku AuahDark hace 3 años
padre
commit
4ac3b669b6

+ 7 - 1
src/android/AndroidClient.cpp

@@ -97,6 +97,7 @@ HTTPSClient::Reply AndroidClient::request(const HTTPSClient::Request &req)
 
 
 	jmethodID constructor = env->GetMethodID(httpsClass, "<init>", "()V");
 	jmethodID constructor = env->GetMethodID(httpsClass, "<init>", "()V");
 	jmethodID setURL = env->GetMethodID(httpsClass, "setUrl", "(Ljava/lang/String;)V");
 	jmethodID setURL = env->GetMethodID(httpsClass, "setUrl", "(Ljava/lang/String;)V");
+	jmethodID setMethod = env->GetMethodID(httpsClass, "setMethod", "(Ljava/lang/String;)V");
 	jmethodID request = env->GetMethodID(httpsClass, "request", "()Z");
 	jmethodID request = env->GetMethodID(httpsClass, "request", "()Z");
 	jmethodID getInterleavedHeaders = env->GetMethodID(httpsClass, "getInterleavedHeaders", "()[Ljava/lang/String;");
 	jmethodID getInterleavedHeaders = env->GetMethodID(httpsClass, "getInterleavedHeaders", "()[Ljava/lang/String;");
 	jmethodID getResponse = env->GetMethodID(httpsClass, "getResponse", "()[B");
 	jmethodID getResponse = env->GetMethodID(httpsClass, "getResponse", "()[B");
@@ -109,8 +110,13 @@ HTTPSClient::Reply AndroidClient::request(const HTTPSClient::Request &req)
 	env->CallVoidMethod(httpsObject, setURL, url);
 	env->CallVoidMethod(httpsObject, setURL, url);
 	env->DeleteLocalRef(url);
 	env->DeleteLocalRef(url);
 
 
+	// Set method
+	jstring method = env->NewStringUTF(req.method.c_str());
+	env->CallVoidMethod(httpsObject, setMethod, method);
+	env->DeleteLocalRef(method);
+
 	// Set post data
 	// Set post data
-	if (req.method == Request::POST)
+	if (req.postdata.size() > 0)
 	{
 	{
 		jmethodID setPostData = env->GetMethodID(httpsClass, "setPostData", "([B)V");
 		jmethodID setPostData = env->GetMethodID(httpsClass, "setPostData", "([B)V");
 		jbyteArray byteArray = env->NewByteArray((jsize) req.postdata.length());
 		jbyteArray byteArray = env->NewByteArray((jsize) req.postdata.length());

+ 21 - 1
src/android/java/org/love2d/luahttps/LuaHTTPS.java

@@ -11,6 +11,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.OutputStream;
 import java.net.HttpURLConnection;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.MalformedURLException;
+import java.net.ProtocolException;
 import java.net.URL;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashMap;
@@ -22,6 +23,7 @@ class LuaHTTPS {
     static private String TAG = "LuaHTTPS";
     static private String TAG = "LuaHTTPS";
 
 
     private String urlString;
     private String urlString;
+    private String method;
     private byte[] postData;
     private byte[] postData;
     private byte[] response;
     private byte[] response;
     private int responseCode;
     private int responseCode;
@@ -34,6 +36,7 @@ class LuaHTTPS {
 
 
     public void reset() {
     public void reset() {
         urlString = null;
         urlString = null;
+        method = "GET";
         postData = null;
         postData = null;
         response = null;
         response = null;
         responseCode = 0;
         responseCode = 0;
@@ -50,6 +53,11 @@ class LuaHTTPS {
         this.postData = postData;
         this.postData = postData;
     }
     }
 
 
+    @Keep
+    public void setMethod(String method) {
+        this.method = method.toUpperCase();
+    }
+
     @Keep
     @Keep
     public void addHeader(String key, String value) {
     public void addHeader(String key, String value) {
         headers.put(key, value);
         headers.put(key, value);
@@ -110,13 +118,21 @@ class LuaHTTPS {
             return false;
             return false;
         }
         }
 
 
+        // Set request method
+        try {
+            connection.setRequestMethod(method);
+        } catch (ProtocolException e) {
+            Log.e(TAG, "Error", e);
+            return false;
+        }
+
         // Set header
         // Set header
         for (Map.Entry<String, String> headerData: headers.entrySet()) {
         for (Map.Entry<String, String> headerData: headers.entrySet()) {
             connection.setRequestProperty(headerData.getKey(), headerData.getValue());
             connection.setRequestProperty(headerData.getKey(), headerData.getValue());
         }
         }
 
 
         // Set post data
         // Set post data
-        if (postData != null) {
+        if (postData != null && canSendData()) {
             connection.setDoOutput(true);
             connection.setDoOutput(true);
             connection.setChunkedStreamingMode(0);
             connection.setChunkedStreamingMode(0);
 
 
@@ -168,4 +184,8 @@ class LuaHTTPS {
         connection.disconnect();
         connection.disconnect();
         return true;
         return true;
     }
     }
+
+    private boolean canSendData() {
+        return !method.equals("GET") && !method.equals("HEAD");
+    }
 }
 }