add: http.postMultipart
This commit is contained in:
@@ -7,6 +7,16 @@ import android.support.test.runner.AndroidJUnit4;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.MultipartBody;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
@@ -18,9 +28,6 @@ import static org.junit.Assert.assertEquals;
|
||||
public class ExampleInstrumentedTest {
|
||||
@Test
|
||||
public void useAppContext() throws Exception {
|
||||
// Context of the app under test.
|
||||
Context appContext = InstrumentationRegistry.getTargetContext();
|
||||
|
||||
assertEquals("com.stardust.autojs.test", appContext.getPackageName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,14 @@ module.exports = function(runtime, scope){
|
||||
return http.post(url, data, options, callback);
|
||||
}
|
||||
|
||||
http.postMultipart = function(url, files, options, callback){
|
||||
options = options || {};
|
||||
options.method = "POST";
|
||||
options.contentType = "multipart/form-data";
|
||||
options.files = files;
|
||||
return http.request(url, options, callback);
|
||||
}
|
||||
|
||||
http.request = function(url, options, callback){
|
||||
var call = http.client().newCall(http.buildRequest(url, options));
|
||||
if(!callback){
|
||||
@@ -56,12 +64,53 @@ module.exports = function(runtime, scope){
|
||||
}
|
||||
if(options.body){
|
||||
r.method(options.method, parseBody(options, options.body));
|
||||
}else{
|
||||
}else if(options.files){
|
||||
r.method(options.method, parseMultipart(options.files));
|
||||
}else {
|
||||
r.method(options.method, null);
|
||||
}
|
||||
return r.build();
|
||||
}
|
||||
|
||||
function parseMultipart(files){
|
||||
var builder = new MultipartBody.Builder()
|
||||
.setType(MultipartBody.FORM);
|
||||
for(var key in files){
|
||||
if(!files.hasOwnProperty(key)){
|
||||
continue;
|
||||
}
|
||||
var value = files[key];
|
||||
if(typeof(value) == 'string'){
|
||||
builder.addFormDataPart(key, value);
|
||||
continue;
|
||||
}
|
||||
var path, mimeType, fileName;
|
||||
if(typeof(value.getPath) == 'function'){
|
||||
path = value.getPath();
|
||||
}else if(value.length == 2){
|
||||
fileName = value[0];
|
||||
path = value[1];
|
||||
}else if(value.length >= 3){
|
||||
fileName = value[0];
|
||||
mimeType = value[1]
|
||||
path = value[2];
|
||||
}
|
||||
var file = new com.stardust.pio.PFile(path);
|
||||
fileName = fileName || file.getName();
|
||||
mimeType = mimeType || parseMimeType(file.getExtension());
|
||||
builder.addFormDataPart(key, fileName, RequestBody.create(MediaType.parse(mimeType), file));
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
function parseMimeType(ext){
|
||||
if(ext.length == 0){
|
||||
return "application/octet-stream";
|
||||
}
|
||||
return android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext)
|
||||
|| "application/octet-stream";
|
||||
}
|
||||
|
||||
function fillPostData(options, data){
|
||||
if(options.contentType == "application/x-www-form-urlencoded"){
|
||||
var b = new FormBody.Builder();
|
||||
@@ -89,6 +138,8 @@ module.exports = function(runtime, scope){
|
||||
function parseBody(options, body){
|
||||
if(typeof(body) == "string"){
|
||||
body = RequestBody.create(MediaType.parse(options.contentType), body);
|
||||
}else if(body instanceof RequestBody){
|
||||
return body;
|
||||
}else{
|
||||
body = new RequestBody({
|
||||
contentType: function(){
|
||||
|
||||
@@ -51,6 +51,7 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
||||
mAndroidContext = context;
|
||||
mContext = createContext();
|
||||
mScriptable = createScope(mContext);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,7 +1,21 @@
|
||||
package com.stardust.autojs;
|
||||
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import com.stardust.pio.PFiles;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.MultipartBody;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
@@ -11,8 +25,21 @@ public class ExampleUnitTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
public void test() throws IOException {
|
||||
MimeTypeMap.getSingleton().getMimeTypeFromExtension();
|
||||
File file = new File("C:\\Users\\Stardust\\Desktop\\1.txt");
|
||||
System.out.println(PFiles.read(file));
|
||||
String url = "http://posttestserver.com/post.php?dir=example";
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
RequestBody formBody = new MultipartBody.Builder()
|
||||
.setType(MultipartBody.FORM)
|
||||
.addFormDataPart("file", file.getName(),
|
||||
RequestBody.create(MediaType.parse("text/plain"), file))
|
||||
.addFormDataPart("other_field", "other_field_value")
|
||||
.build();
|
||||
Request request = new Request.Builder().url(url).post(formBody).build();
|
||||
Response response = client.newCall(request).execute();
|
||||
System.out.println(response.body().string());
|
||||
}
|
||||
|
||||
public boolean equals(int i, int j) {
|
||||
|
||||
Reference in New Issue
Block a user