我想将图像发送到android中的REST API。我创建了一个Android应用程序,它只是对API进行请求调用并返回响应,但我知道我必须将Image发布到REST API。
这是我的代码。
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();
}
}
如何发布帖子请求以将图片发布到REST API。这是休息API代码。
@Path("/files")
public class JerseyFileUpload {
@POST
@Path("/upload")
@Consumes(MediaType.)
@Produces(MediaType.APPLICATION_JSON)
public ImageUrl responseMsg(){
//do something
}
使用哪种媒体类型来消费图像。
答案 0 :(得分:1)
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();
}
答案 1 :(得分:0)
我个人使用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;
}
答案 2 :(得分:0)
您实施服务并在其中发布图片,请查看我的示例here
检查 postMultiPart 方法。
答案 3 :(得分:0)
将其更改为: 多部分/格式数据