Skip to content

Commit 4ac3b66

Browse files
committed
Fix Android compile.
1 parent d05fd9b commit 4ac3b66

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/android/AndroidClient.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ HTTPSClient::Reply AndroidClient::request(const HTTPSClient::Request &req)
9797

9898
jmethodID constructor = env->GetMethodID(httpsClass, "<init>", "()V");
9999
jmethodID setURL = env->GetMethodID(httpsClass, "setUrl", "(Ljava/lang/String;)V");
100+
jmethodID setMethod = env->GetMethodID(httpsClass, "setMethod", "(Ljava/lang/String;)V");
100101
jmethodID request = env->GetMethodID(httpsClass, "request", "()Z");
101102
jmethodID getInterleavedHeaders = env->GetMethodID(httpsClass, "getInterleavedHeaders", "()[Ljava/lang/String;");
102103
jmethodID getResponse = env->GetMethodID(httpsClass, "getResponse", "()[B");
@@ -109,8 +110,13 @@ HTTPSClient::Reply AndroidClient::request(const HTTPSClient::Request &req)
109110
env->CallVoidMethod(httpsObject, setURL, url);
110111
env->DeleteLocalRef(url);
111112

113+
// Set method
114+
jstring method = env->NewStringUTF(req.method.c_str());
115+
env->CallVoidMethod(httpsObject, setMethod, method);
116+
env->DeleteLocalRef(method);
117+
112118
// Set post data
113-
if (req.method == Request::POST)
119+
if (req.postdata.size() > 0)
114120
{
115121
jmethodID setPostData = env->GetMethodID(httpsClass, "setPostData", "([B)V");
116122
jbyteArray byteArray = env->NewByteArray((jsize) req.postdata.length());

src/android/java/org/love2d/luahttps/LuaHTTPS.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.OutputStream;
1212
import java.net.HttpURLConnection;
1313
import java.net.MalformedURLException;
14+
import java.net.ProtocolException;
1415
import java.net.URL;
1516
import java.util.ArrayList;
1617
import java.util.HashMap;
@@ -22,6 +23,7 @@ class LuaHTTPS {
2223
static private String TAG = "LuaHTTPS";
2324

2425
private String urlString;
26+
private String method;
2527
private byte[] postData;
2628
private byte[] response;
2729
private int responseCode;
@@ -34,6 +36,7 @@ public LuaHTTPS() {
3436

3537
public void reset() {
3638
urlString = null;
39+
method = "GET";
3740
postData = null;
3841
response = null;
3942
responseCode = 0;
@@ -50,6 +53,11 @@ public void setPostData(byte[] postData) {
5053
this.postData = postData;
5154
}
5255

56+
@Keep
57+
public void setMethod(String method) {
58+
this.method = method.toUpperCase();
59+
}
60+
5361
@Keep
5462
public void addHeader(String key, String value) {
5563
headers.put(key, value);
@@ -110,13 +118,21 @@ public boolean request() {
110118
return false;
111119
}
112120

121+
// Set request method
122+
try {
123+
connection.setRequestMethod(method);
124+
} catch (ProtocolException e) {
125+
Log.e(TAG, "Error", e);
126+
return false;
127+
}
128+
113129
// Set header
114130
for (Map.Entry<String, String> headerData: headers.entrySet()) {
115131
connection.setRequestProperty(headerData.getKey(), headerData.getValue());
116132
}
117133

118134
// Set post data
119-
if (postData != null) {
135+
if (postData != null && canSendData()) {
120136
connection.setDoOutput(true);
121137
connection.setChunkedStreamingMode(0);
122138

@@ -168,4 +184,8 @@ public boolean request() {
168184
connection.disconnect();
169185
return true;
170186
}
187+
188+
private boolean canSendData() {
189+
return !method.equals("GET") && !method.equals("HEAD");
190+
}
171191
}

0 commit comments

Comments
 (0)