use of org.apache.http.entity.mime.MultipartEntity in project UltimateAndroid by cymcsg.
the class HttpUtils method uploadFilesMPE.
/**
* Update Files via Multipart
*
* @param url
* @param paramsList
* @param fileParams
* @param file
* @param files
* @return status
* @deprecated
*/
public static String uploadFilesMPE(String url, List<NameValuePair> paramsList, String fileParams, File file, File... files) {
String result = "";
try {
DefaultHttpClient mHttpClient;
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);
mHttpClient = new DefaultHttpClient(params);
HttpPost httpPost = new HttpPost(url);
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
if (BasicUtils.judgeNotNull(paramsList)) {
for (NameValuePair nameValuePair : paramsList) {
multipartEntity.addPart(nameValuePair.getName(), new StringBody(nameValuePair.getValue()));
}
}
multipartEntity.addPart(fileParams, new FileBody(file));
for (File f : files) {
multipartEntity.addPart(fileParams, new FileBody(f));
}
httpPost.setEntity(multipartEntity);
HttpResponse httpResp = mHttpClient.execute(httpPost);
// 判断是够请求成功
if (httpResp.getStatusLine().getStatusCode() == 200) {
// 获取返回的数据
result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
Logs.d("HttpPost success :");
Logs.d(result);
} else {
Logs.d("HttpPost failed" + " " + httpResp.getStatusLine().getStatusCode() + " " + EntityUtils.toString(httpResp.getEntity(), "UTF-8"));
result = "HttpPost failed";
}
} catch (ConnectTimeoutException e) {
result = "ConnectTimeoutException";
Logs.e("HttpPost overtime: " + "");
} catch (Exception e) {
e.printStackTrace();
Logs.e(e, "");
result = "Exception";
}
return result;
}
use of org.apache.http.entity.mime.MultipartEntity in project sling by apache.
the class ValidationServiceIT method testValidRequestModel1.
@Test
public void testValidRequestModel1() throws IOException, JsonException {
final String url = String.format("http://localhost:%s", httpPort());
final RequestBuilder requestBuilder = new RequestBuilder(url);
MultipartEntity entity = new MultipartEntity();
entity.addPart("sling:resourceType", new StringBody("validation/test/resourceType1"));
entity.addPart("field1", new StringBody("HELLOWORLD"));
entity.addPart("field2", new StringBody("30.01.1988"));
entity.addPart(SlingPostConstants.RP_OPERATION, new StringBody("validation"));
RequestExecutor re = requestExecutor.execute(requestBuilder.buildPostRequest("/validation/testing/fakeFolder1/resource").withEntity(entity)).assertStatus(200);
String content = re.getContent();
JsonObject jsonResponse = Json.createReader(new StringReader(content)).readObject();
assertTrue(jsonResponse.getBoolean("valid"));
}
use of org.apache.http.entity.mime.MultipartEntity in project sling by apache.
the class ValidationServiceIT method testInvalidRequestModel1.
@Test
public void testInvalidRequestModel1() throws IOException, JsonException {
MultipartEntity entity = new MultipartEntity();
entity.addPart("sling:resourceType", new StringBody("validation/test/resourceType1"));
entity.addPart("field1", new StringBody("Hello World"));
entity.addPart(SlingPostConstants.RP_OPERATION, new StringBody("validation"));
final String url = String.format("http://localhost:%s", httpPort());
RequestBuilder requestBuilder = new RequestBuilder(url);
RequestExecutor re = requestExecutor.execute(requestBuilder.buildPostRequest("/validation/testing/fakeFolder1/resource").withEntity(entity)).assertStatus(200);
String content = re.getContent();
JsonObject jsonResponse = Json.createReader(new StringReader(content)).readObject();
assertFalse(jsonResponse.getBoolean("valid"));
JsonObject failure = jsonResponse.getJsonArray("failures").getJsonObject(0);
assertEquals("Property does not match the pattern \"^\\p{Upper}+$\".", failure.getString("message"));
assertEquals("field1", failure.getString("location"));
assertEquals(10, failure.getInt("severity"));
failure = jsonResponse.getJsonArray("failures").getJsonObject(1);
assertEquals("Missing required property with name \"field2\".", failure.getString("message"));
// location is empty as the property is not found (property name is part of the message rather)
assertEquals("", failure.getString("location"));
assertEquals(0, failure.getInt("severity"));
}
use of org.apache.http.entity.mime.MultipartEntity in project android-instagram by markchang.
the class TakePictureActivity method doUpload.
// fixme: this doesn't need to be a Map return
public Map<String, String> doUpload() {
Log.i(TAG, "Upload");
Long timeInMilliseconds = System.currentTimeMillis() / 1000;
String timeInSeconds = timeInMilliseconds.toString();
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
Map returnMap = new HashMap<String, String>();
// check for cookies
if (httpClient.getCookieStore() == null) {
returnMap.put("result", "Not logged in");
return returnMap;
}
try {
// create multipart data
File imageFile = new File(processedImageUri.getPath());
FileBody partFile = new FileBody(imageFile);
StringBody partTime = new StringBody(timeInSeconds);
multipartEntity.addPart("photo", partFile);
multipartEntity.addPart("device_timestamp", partTime);
} catch (Exception e) {
Log.e(TAG, "Error creating mulitpart form: " + e.toString());
returnMap.put("result", "Error creating mulitpart form: " + e.toString());
return returnMap;
}
// upload
try {
HttpPost httpPost = new HttpPost(Utils.UPLOAD_URL);
httpPost.setEntity(multipartEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
Log.i(TAG, "Upload status: " + httpResponse.getStatusLine());
// test result code
if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Log.e(TAG, "Login HTTP status fail: " + httpResponse.getStatusLine().getStatusCode());
returnMap.put("result", "HTTP status error: " + httpResponse.getStatusLine().getStatusCode());
return returnMap;
}
/*
{"status": "ok"}
*/
if (httpEntity != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(httpEntity.getContent(), "UTF-8"));
String json = reader.readLine();
JSONTokener jsonTokener = new JSONTokener(json);
JSONObject jsonObject = new JSONObject(jsonTokener);
Log.i(TAG, "JSON: " + jsonObject.toString());
String loginStatus = jsonObject.getString("status");
if (!loginStatus.equals("ok")) {
Log.e(TAG, "JSON status not ok: " + jsonObject.getString("status"));
returnMap.put("result", "JSON status not ok: " + jsonObject.getString("status"));
return returnMap;
}
}
} catch (Exception e) {
Log.e(TAG, "HttpPost exception: " + e.toString());
returnMap.put("result", "HttpPost exception: " + e.toString());
return returnMap;
}
// configure / comment
try {
HttpPost httpPost = new HttpPost(Utils.CONFIGURE_URL);
String partComment = txtCaption.getText().toString();
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("device_timestamp", timeInSeconds));
postParams.add(new BasicNameValuePair("caption", partComment));
httpPost.setEntity(new UrlEncodedFormEntity(postParams, HTTP.UTF_8));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// test result code
if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Log.e(TAG, "Upload comment fail: " + httpResponse.getStatusLine().getStatusCode());
returnMap.put("result", "Upload comment fail: " + httpResponse.getStatusLine().getStatusCode());
return returnMap;
}
returnMap.put("result", "ok");
return returnMap;
} catch (Exception e) {
Log.e(TAG, "HttpPost comment error: " + e.toString());
returnMap.put("result", "HttpPost comment error: " + e.toString());
return returnMap;
}
}
use of org.apache.http.entity.mime.MultipartEntity in project felix by apache.
the class ITScriptConsolePlugin method execute.
private void execute(ContentBody code) throws Exception {
RequestBuilder rb = new RequestBuilder(ServerConfiguration.getServerUrl());
final MultipartEntity entity = new MultipartEntity();
// Add Sling POST options
entity.addPart("lang", new StringBody("groovy"));
entity.addPart("code", code);
executor.execute(rb.buildPostRequest("/system/console/sc").withEntity(entity).withCredentials("admin", "admin")).assertStatus(200);
}
Aggregations