在android中将图像发布到RESTAPI
我想把图像发送到android中的RESTAPI。我创建了一个android应用程序,它只需对API进行请求调用并返回响应,但我知道我必须将图像发送到RESTAPI 这是我的密码在android中将图像发布到RESTAPI,android,api,rest,android-asynctask,Android,Api,Rest,Android Asynctask,我想把图像发送到android中的RESTAPI。我创建了一个android应用程序,它只需对API进行请求调用并返回响应,但我知道我必须将图像发送到RESTAPI 这是我的密码 private class PostImage extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... data) { //
private class PostImage extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... data) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
URL url = new URL("http://localhost:8080/JAXRS-FileUpload/rest/files/upload");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
}
}
应该使用哪种媒体类型来使用图像。我个人使用loopj进行客户机-服务器通信,可以使用其中的文件发送图像。在应用程序gradle中添加以下依赖项以使用loopj
compile 'com.loopj.android:android-async-http:1.4.9'
然后发送图像
public void sendAllData() {
String Url = "your url here";
//If any auth is needed
String username = "username";
String password = "password";
Cursor cursor = mContext.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC");
if (cursor != null && cursor.moveToLast()) {
Uri fileURI = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
fileSrc = fileURI.toString();
cursor.close();
}
// Bitmap compressedImage = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
AsyncHttpClient client = new AsyncHttpClient();
client.setBasicAuth(username, password);
RequestParams params = new RequestParams();
try {
params.put("pic", storeImage());
} catch (FileNotFoundException e) {
Log.d("MyApp", "File not found!!!" + fileSrc);
}
client.post(Url, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject responseBody) {
//Do what's needed
}
@Override
public void onFailure(int statusCode, Header[] headers, String errorResponse, Throwable error) {
//Print error
}
});
}
private File storeImage() {
String filename = "anyName";
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
OutputStream outStream = null;
File file = new File(extStorageDirectory, filename + ".jpg");
try {
outStream = new FileOutputStream(file);
bookImage.compress(CompressFormat.JPEG, 80, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
您实现了一个服务并在其中发布了图像,请检查我的示例 检查多部分后的方法。将其更改为:
MultipartEntity entity = new MultipartEntity();
FileBody userPhoto = new FileBody(new File(path));
entity.addPart("file", userPhoto);
String url = UPDATE_PROFILE_URL;
HttpParams params1 = new BasicHttpParams();
HttpConnectionParams.setStaleCheckingEnabled(params1, false);
int timeOut = 2 * 60 * 1000;
HttpConnectionParams.setConnectionTimeout(params1, timeOut);
HttpConnectionParams.setSoTimeout(params1, timeOut);
DefaultHttpClient httpClient = new DefaultHttpClient(params1);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity);
HttpResponse response = null;
JSONObject jsResp = null;
try {
response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
String sServerResponse = EntityUtils.toString(resEntity);
Log.i("abc", "FROM: POST:" + sServerResponse);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
多部分/表单数据如何在服务器上接收该图像?表示我应该在REST API中使用哪种媒体类型?在这种情况下,web服务上的媒体类型将是MediaType.multipart\u form\u data我希望通过imageview显示许多图像。例如,我有一个包含10个索引的数组,在循环中,我希望显示10个图像。我如何才能做到这一点。您有URL在阵列中?
MultipartEntity entity = new MultipartEntity();
FileBody userPhoto = new FileBody(new File(path));
entity.addPart("file", userPhoto);
String url = UPDATE_PROFILE_URL;
HttpParams params1 = new BasicHttpParams();
HttpConnectionParams.setStaleCheckingEnabled(params1, false);
int timeOut = 2 * 60 * 1000;
HttpConnectionParams.setConnectionTimeout(params1, timeOut);
HttpConnectionParams.setSoTimeout(params1, timeOut);
DefaultHttpClient httpClient = new DefaultHttpClient(params1);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity);
HttpResponse response = null;
JSONObject jsResp = null;
try {
response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
String sServerResponse = EntityUtils.toString(resEntity);
Log.i("abc", "FROM: POST:" + sServerResponse);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}