-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add http get/post request support for Android through JNI.
- Loading branch information
1 parent
7132445
commit f8dbb64
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
|
||
/* Copy this to <AndroidProject>/app/src/main/java/org/libsdl/app/MediaHelperAndroid.java */ | ||
|
||
package org.libsdl.app; | ||
|
||
import android.content.Context; | ||
|
||
import java.lang.Class; | ||
import java.lang.reflect.Method; | ||
|
||
import java.io.InputStreamReader; | ||
import java.io.BufferedReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.io.OutputStreamWriter; | ||
import java.io.BufferedWriter; | ||
|
||
public class MediaHelperAndroid { | ||
|
||
public static String httpRequest(String urlstr, String data) throws Exception { | ||
String content = "", line; | ||
try { | ||
URL url = new URL(urlstr); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setDoOutput(true); | ||
if (data == null) { | ||
connection.setRequestMethod("GET"); | ||
} else { | ||
connection.setRequestMethod("POST"); | ||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream())); | ||
writer.write(data); | ||
writer.flush(); | ||
writer.close(); | ||
} | ||
connection.setConnectTimeout(5000); | ||
connection.setReadTimeout(5000); | ||
connection.connect(); | ||
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); | ||
while ((line = rd.readLine()) != null) { | ||
content += line + "\n"; | ||
} | ||
} catch (Exception ex) { | ||
return ex.getMessage(); | ||
} | ||
return content; | ||
} | ||
} |
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