I tried to use this Android Picasso library, How to add authentication headers? to access a protected image that returns the base64 version of the image. My problem is that the picasso always failed. and I don't know why. the authorization code is valid since the profile details are loaded. only the image was not. Here is my implementation how to get the image.
public class PicaAuth {
private static Picasso sPicasso;
private PicaAuth() {
}
public static Picasso getImageLoader(final Context context) {
if (sPicasso == null) {
Picasso.Builder builder = new Picasso.Builder(context);
builder.downloader(new CustomOkHttpDownloader(context));
sPicasso = builder.build();
}
return sPicasso;
}
private static class CustomOkHttpDownloader extends OkHttpDownloader {
public CustomOkHttpDownloader(Context context) {
super(context);
}
#Override
protected HttpURLConnection openConnection(final Uri uri) throws IOException {
HttpURLConnection connection = super.openConnection(uri);
connection.setRequestProperty("Authorization", Auth.getBearerAccessToken());
return connection;
}
}
}
Main Activity
PicaAuth.getImageLoader(MainActivity.this)
.load(uri)
.into(mImage, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
Log.d("Image Success");
}
#Override
public void onError() {
Log.e("Image Failed");
}
});
You need to intercept the answer and change it
OkHttpClient client;
OkHttpClient.Builder builderOkHttpClient;
builderOkHttpClient = new OkHttpClient.Builder();
builderOkHttpClient.addInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.build();
Response response = chain.proceed(newRequest);
try {
MediaType contentType = response.body().contentType();
String base64String = response.body().string().getBytes("UTF-8");
base64String = base64String .replace("data:image/jpeg;base64,", "");
byte[] decodedString = Base64.decode(base64String , Base64.DEFAULT);
ResponseBody body = ResponseBody.create(contentType, decodedString);
response = response.newBuilder().body(body).build();
} catch (JSONException e) {
e.printStackTrace();
}
return response;
}
});
int cacheSize = 10 * 1024 * 1024;
Cache cache = new Cache(context.getCacheDir(), cacheSize);
builderOkHttpClient.cache(cache);
client = builderOkHttpClient.build();
Application.getAppComponent().inject(this);
picasso = new Picasso.Builder(context)
.downloader(new OkHttp3Downloader(client))
.loggingEnabled(true)
.indicatorsEnabled(true)
.listener(new Picasso.Listener() {
#Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
Log.e("PICASSO", "loading image " + uri);
Log.e("PICASSO ERROR", exception.getMessage());
}
}
).build();
Above answer works great. Then if the base 64 encoded image is further stored inside a JSON Object.
String jsonData = response.body().string();
JSONObject Jobject = new JSONObject(jsonData);
String base64String = (String) Jobject.get("ImageData");
Related
How to check if the URL is an image URL that must be either PNG, GIF, JPG formats
I see that it can be done with this code:
URLConnection connection = new URL("http://foo.bar/w23afv").openConnection();
String contentType = connection.getHeaderField("Content-Type");
boolean image = contentType.startsWith("image/");
But, I need to check using either Glide or OKHttpClient.
How to achieve this using two techniques mentioned above?
If all you want to do is check the Content-Type of a URL, without actually downloading the content, an HTTP HEAD request would be appropriate.
The HEAD method is identical to GET except that the server MUST NOT
return a message-body in the response. The metainformation contained
in the HTTP headers in response to a HEAD request SHOULD be identical
to the information sent in response to a GET request. This method can
be used for obtaining metainformation about the entity implied by the
request without transferring the entity-body itself. This method is
often used for testing hypertext links for validity, accessibility,
and recent modification.
You can do this with OkHttp as follows:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://foo.bar/w23afv")
.head()
.build();
try {
Response response = client.newCall(request).execute();
String contentType = response.header("Content-Type");
boolean image = false;
if (contentType != null) {
image = contentType.startsWith("image/");
}
} catch (IOException e) {
// handle error
}
If you are okay with the HEAD request I think that Jeff Lockhart is the cleanest solution. Anyway I post here below a more comprehensive solution about your question:
With okhttp3 only
implementation 'com.squareup.okhttp3:okhttp:3.14.0'
You could check headers of an HEAD request also accessing body ContentType.
Check headers onto onResponse()
OkHttpClient client = new OkHttpClient();
Request requestHead = new Request.Builder()
.url("your tiny url")
.head()
.build();
Request request = new Request.Builder()
.url("your tiny url")
.build();
// HEAD REQUEST
client.newCall(requestHead).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
Log.d("OKHTTP3 onFailure", e.getMessage());
}
#Override
public void onResponse(#NonNull Call call, #NonNull Response response) throws IOException {
try {
final ResponseBody _body = response.body();
if (_body != null) {
final MediaType _contentType = _body.contentType();
if (_contentType != null) {
final String _mainType = _contentType.type(); // image
final String _subtypeType = _contentType.subtype(); // jpeg/png/etc.
Log.d("OKHTTP3 - media content type", _contentType.toString());
Log.d("OKHTTP3 - media main type", _mainType);
Log.d("OKHTTP3 - media sub type", _subtypeType);
boolean isImage = _mainType.equals("image");
Log.d("OKHTTP3 - I'VE GOT AN IMAGE", "" + isImage);
if (isImage) {
Log.d("OKHTTP3 WE HAVE AN IMAGE!", "yay!");
} else {
Log.d("OKHTTP3 SKIP CONTENT!", "Buuu!");
}
}
}
} catch (Exception e) {
Log.d("OKHTTP3 Interrupted Exception", e.getMessage());
}
}
});
// GET REQUEST
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
Log.d("OKHTTP3 onFailure", e.getMessage());
}
#Override
public void onResponse(Call call, Response response) throws IOException {
try {
final ResponseBody _body = response.body();
final MediaType _contentType = _body.contentType();
final String _mainType = _contentType.type(); // image
final String _subtypeType = _contentType.subtype(); // jpeg/png/etc.
Log.d("OKHTTP3 - media content type", _contentType.toString());
Log.d("OKHTTP3 - media main type", _mainType);
Log.d("OKHTTP3 - media sub type", _subtypeType);
boolean isImage = _mainType.equals("image");
Log.d("OKHTTP3 - I'VE GOT AN IMAGE", "" + isImage);
if (isImage) {
final InputStream inputStream = response.body().byteStream();
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
runOnUiThread(() -> {
helloImageView.setImageBitmap(bitmap);
});
}
} catch (Exception e) {
Log.d("OKHTTP3 Interrupted Exception", e.getMessage());
}
}
});
Check headers with interceptor:
Interceptors is good because it centralises in a single place where you check your url.
OkHttpClient clientWithInterceptor = new OkHttpClient.Builder()
.addInterceptor(chain -> {
Response _response = chain.proceed(request);
final ResponseBody _body = _response.body();
if (_body != null) {
final MediaType _contentType = _body.contentType();
if (_contentType != null) {
final String _mainType = _contentType.type(); // image
final String _subtypeType = _contentType.subtype(); // jpeg/png/etc.
Log.d("OKHTTP3 - media content type", _contentType.toString());
Log.d("OKHTTP3 - media main type", _mainType);
Log.d("OKHTTP3 - media sub type", _subtypeType);
boolean isImage = _mainType.equals("image");
Log.d("OKHTTP3 - I'VE GOT AN IMAGE", "" + isImage);
if (isImage) {
return _response;
} else {
return return415Response(chain);
}
} else {
return return415Response(chain);
}
} else {
return return415Response(chain);
}
}).build();
clientWithInterceptor.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
Log.d("OKHTTP3 onFailure", e.getMessage());
}
#Override
public void onResponse(Call call, Response response) throws IOException {
Log.d("OKHTTP3 - onResponse", "" + response.toString());
if (response.isSuccessful()) {
final InputStream inputStream = response.body().byteStream();
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
runOnUiThread(() -> {
helloImageView.setImageBitmap(bitmap);
});
}
}
});
//*/
}
private Response return415Response(Interceptor.Chain chain) {
return new Response.Builder()
.code(415) // Media type not supported... or whatever
.protocol(Protocol.HTTP_1_1)
.message("Media type not supported")
.body(ResponseBody.create(MediaType.parse("text/html"), ""))
.request(chain.request())
.build();
}
Using Glide v4 along with okhttp3
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
implementation 'com.github.bumptech.glide:annotations:4.9.0'
implementation "com.github.bumptech.glide:okhttp3-integration:4.9.0"
You need to extend GlideAppModule
#GlideModule
public class OkHttpAppGlideModule extends AppGlideModule {
#Override
public void applyOptions(#NonNull Context context, #NonNull GlideBuilder builder) {
super.applyOptions(context, builder);
}
#Override
public void registerComponents(#NonNull Context context, #NonNull Glide glide, #NonNull Registry registry) {
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(15, TimeUnit.SECONDS)
.connectTimeout(15, TimeUnit.SECONDS)
.addNetworkInterceptor(chain -> {
Response _response = chain.proceed(chain.request());
int _httpResponseCode = _response.code();
if (_httpResponseCode == 301
|| _httpResponseCode == 302
|| _httpResponseCode == 303
|| _httpResponseCode == 307) {
return _response; // redirect
}
final ResponseBody _body = _response.body();
if (_body != null) {
final MediaType _contentType = _body.contentType();
if (_contentType != null) {
final String _mainType = _contentType.type(); // image
final String _subtypeType = _contentType.subtype(); // jpeg/png/etc.
Log.d("OKHTTP3 - media content type", _contentType.toString());
Log.d("OKHTTP3 - media main type", _mainType);
Log.d("OKHTTP3 - media sub type", _subtypeType);
boolean isImage = _mainType.equals("image");
Log.d("OKHTTP3 - I'VE GOT AN IMAGE", "" + isImage);
if (isImage) {
Log.d("OKHTTP3 WE HAVE AN IMAGE!", "yay!");
return _response;
} else {
return return415Response(chain);
}
} else {
return return415Response(chain);
}
} else {
return return415Response(chain);
}
}).build();
OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(client);
registry.replace(GlideUrl.class, InputStream.class, factory);
}
private Response return415Response(Interceptor.Chain chain) {
return new Response.Builder()
.code(415) // Media type not supported... or whatever
.protocol(Protocol.HTTP_1_1)
.message("Media type not supported")
.body(ResponseBody.create(MediaType.parse("text/html"), ""))
.request(chain.request())
.build();
}
Then calling
Glide.with(this)
.load("your tini url")
.into(helloImageView);
You enter your okhttp client interceptor and you can act accordingly.
in okHttpClient you have to use below line as a URL and make API Call, if call successful then you can check your condition.
ex:-
String url = new URL("http://foo.bar/w23afv").toString();
OkHttpHandler okHttpHandler= new OkHttpHandler();
okHttpHandler.execute(url);
If you obtain the image string. you can simply check for that image url that ends with (jpg or png) using this String format method.
imageString.endsWith("jpg") || imageString.endsWith("png")
If you are getting "Image path" as a string then try this...
image_extension = image_path.substring(image_path.length() - 3)
then compare this image_extension with png,jpg and gif
I want to use OkHttp library for networking in Android.
I started with the simple post example as written in their website:
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
With this call:
String response = post("http://www.roundsapp.com/post", json);
This call ends with NetworkOnMainThreadException.
I could wrap the call with an AsyncTask, but as far as I understand from the examples, the OkHttp library should have already taken care of that..
Am I doing something wrong?
You should use OkHttp's async method.
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
Call post(String url, String json, Callback callback) {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Call call = client.newCall(request);
call.enqueue(callback);
return call;
}
And then your response would be handled in the callback (OkHttp 2.x):
post("http://www.roundsapp.com/post", json, new Callback() {
#Override
public void onFailure(Request request, Throwable throwable) {
// Something went wrong
}
#Override public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
String responseStr = response.body().string();
// Do what you want to do with the response.
} else {
// Request not successful
}
}
});
Or OkHttp 3.x/4.x:
post("http://www.roundsapp.com/post", "", new Callback() {
#Override
public void onFailure(Call call, IOException e) {
// Something went wrong
}
#Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String responseStr = response.body().string();
// Do what you want to do with the response.
} else {
// Request not successful
}
}
});
Take a look at their recipes for more examples: http://square.github.io/okhttp/recipes/
According to the OkHttp docs:
It supports both synchronous blocking calls and async calls with callbacks.
Your example is on main thread and Android since version 3.0 throws that exception if you try to do network calls on main thread
Better option is to use it together with retrofit and Gson:
http://square.github.io/retrofit/
https://code.google.com/p/google-gson/
Here are the examples:
http://engineering.meetme.com/2014/03/best-practices-for-consuming-apis-on-android/
http://heriman.net/?p=5
If you follows these steps to implement OKHTTP, then definitely you'll call multiple API on multiple screen by applying only two lines of code
UpdateListener updateListener = new UpdateListener(HitAPIActivity.this, baseHTTPRequest);
updateListener.getJsonData();
Step 1:
baseHTTPRequest = new BaseHTTPRequest();
// baseHTTPRequest.setURL("https://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demohttps://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo");
baseHTTPRequest.setURL("http://jsonparsing.parseapp.com/jsonData/moviesDemoItem.txt");
baseHTTPRequest.setRequestCode(reqType);
baseHTTPRequest.setCachedRequired(true);
UpdateListener updateListener = new UpdateListener(HitAPIActivity.this, baseHTTPRequest);
updateListener.executeRequest();
Step 2 : Create a request class
/**
* Created by Deepak Sharma on 4/7/16.
* This is a HTTP request class which has the basic parameters.
* If you wants to add some more parameters, please make a subclass of that class
* and add with your subclass. Don't modify this class.
*/
public class BaseHTTPRequest<T> {
private Context context;
private String URL;
private int requestCode;
private List<T> listParameters;
private String header;
private boolean isCachedRequired;
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
public void setURL(String URL) {
this.URL = URL;
}
public String getURL() {
return URL;
}
public int getRequestCode() {
return requestCode;
}
public void setRequestCode(int requestCode) {
this.requestCode = requestCode;
}
public List<T> getListParameters() {
return listParameters;
}
public void setListParameters(List<T> listParameters) {
this.listParameters = listParameters;
}
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
public boolean isCachedRequired() {
return isCachedRequired;
}
public void setCachedRequired(boolean cachedRequired) {
isCachedRequired = cachedRequired;
}
}
step 4 : Create a listener class
import android.util.Log;
import com.google.gson.Gson;
import java.io.IOException;
import dxswifi_direct.com.wifidirectcommunication.base.model.request.BaseHTTPRequest;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* Created by Deepak Sharma on 4/7/16.
* #email : dpsharma.sharma1#gmail.com
* This is a Simple java class which will help you for HTTP request/response and it will
* throw the response to your correspondance activity.
*/
public class UpdateListener {
private OnUpdateViewListener onUpdateViewListener;
OkHttpClient okHttpClient = new OkHttpClient();
BaseHTTPRequest mRequestModel;
private String mURL = null;
private Request mRequest = null;
public interface OnUpdateViewListener {
void updateView(String responseString, boolean isSuccess,int reqType);
}
public UpdateListener(OnUpdateViewListener onUpdateView, final BaseHTTPRequest requestModel) {
this.mRequestModel = requestModel;
this.onUpdateViewListener = onUpdateView;
if (requestModel.isCachedRequired())
{
/*File httpCacheDirectory = new File(requestModel.getContext().getCacheDir(), "responses");
Cache cache = null;
cache = new Cache(httpCacheDirectory, 10 * 1024 * 1024);
if (cache != null) {
okHttpClient.setCache(cache);
}*/
}
/*mURL = null;
if (requestModel.getListParameters()!=null && requestModel.getListParameters().size()>0)
{
HttpUrl.Builder urlBuilder = HttpUrl.parse(requestModel.getURL()).newBuilder();
List<RequestParameter> requestParameters = requestModel.getListParameters();
for (int i=0; i<requestParameters.size();i++)
{
urlBuilder.addQueryParameter(requestParameters.get(i).getKey(),requestParameters.get(i).getValue());
}
mURL = urlBuilder.build().toString();
}
else
{
mURL = requestModel.getURL();
}*/
mURL = requestModel.getURL();
if (mRequestModel.getListParameters()!=null && mRequestModel.getListParameters().size()>1)
{
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
mRequest = new Request.Builder()
.url(mURL)
.post(RequestBody.create(JSON, new Gson().toJson(BaseHTTPRequest.class)))
.build();
}
else
{
mRequest = new Request.Builder()
.url(mURL)
.build();
}
}
public void executeRequest()
{
Call call = okHttpClient.newCall(mRequest);
call.enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
onUpdateViewListener.updateView(NetworkException.getErrorMessage(e), false, mRequestModel.getRequestCode());
}
#Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) {
// You can also throw your own custom exception
throw new IOException("Unexpected code " + response);
} else {
Log.i("Response:",response.toString());
Log.i("Response body:",response.body().toString());
Log.i("Response message:",response.message());
onUpdateViewListener.updateView(response.body().string(),true, mRequestModel.getRequestCode());
}
// do something wih the result
}
});
}
}
step 5 : From the activity you requesting, implement listener
public class HitAPIActivity extends AppCompatActivity implements View.OnClickListener, UpdateListener.OnUpdateViewListener{
#Override
public void updateView(final String responseString, boolean isSuccess, int reqType) {
if (isSuccess)
{
if (!responseString.contains("failure")
&& !responseString.contains("Error")) {
// Handle request on the basis of Request Type.
switch (reqType) {
case ApiConstants.GET_CONTACTS:
break;
default:
break;
}
}
}
}
I tried to download image from URL using OkHttp . It returns null.
I have permissions
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Here is my Rest Client
public class RestClient {
private static volatile RestClient instance;
private final OkHttpClient client;
private RestClient(Context context) throws Exception {
client = new OkHttpClient();
}
public static RestClient getInstance(Context context) throws Exception {
if (instance == null)
synchronized (RestClient.class) {
if (instance == null)
instance = new RestClient(context);
}
return instance;
}
public Call uploadPicture(Callback callback){
String link = "http://www.101apps.co.za/images/headers/101_logo_very_small.jpg";
Request request = new Request.Builder()
.url(String.format(link))
.get()
.build();
Call call = client.newCall(request);
call.enqueue(callback);
return call;
}
}
Here is my getPicture method in the main activity
public void getPicByURL() {
try {
RestClient.getInstance(getActivity()).uploadPicture(new Callback() {
#Override
public void onFailure(Request request, IOException e) {
e.printStackTrace();
}
#Override
public void onResponse(Response response) throws IOException {
final String json = response.body().string();
if (response.code() == Constants.SUCCESSFUL_DOWNLOAD) {
ResponseBody in = response.body();
InputStream inputStream = in.byteStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
updateImage(BitmapFactory.decodeStream(bufferedInputStream));
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private void updateImage(final Bitmap bitmap) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
if (bitmap != null) {
mSelectedPic.setImageBitmap(bitmap);
} else {
Toast.makeText(getActivity(), R.string.show_error, Toast.LENGTH_SHORT).show();
}
}
});
}
updateImage() method takes bitmap = null. I don`t know what I doing wrong. Please help.
Hi its work for me try this
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("https://www.101apps.co.za/images/headers/101_logo_very_small.jpg").get().build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
Log.i("Tag","error"+e.getMessage());
}
#Override
public void onResponse(Call call, final Response response) throws IOException {
runOnUiThread(new Runnable() {
#Override
public void run() {
ImageView imageView (ImageView)findViewById(R.id.down_imageView);
ResponseBody in = response.body();
InputStream inputStream = in.byteStream();
Log.i("inputStream","inputstream value = "+inputStream);
// convert inputstram to bufferinoutstream
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
Bitmap bitmap=BitmapFactory.decodeStream(bufferedInputStream);
Log.i("bitmap","bitmap value = "+bitmap);
imageView.setImageBitmap(bitmap);
}
});
}
});
}
});
With your current Url (http://www.101apps.co.za/images/headers/101_logo_very_small.jpg) and your purpose (loading the image into ImageView only), I suggest you use Picasso or Glide instead.
Sample code if using Picasso:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this)
.load("http://www.101apps.co.za/images/headers/101_logo_very_small.jpg")
.into(imageView);
Hope it helps!
I'd like to do some work in AsyncTask including some server requests for downloading small files. When download is done continue logic inside AsyncTask and when all stuff is done I get the result in activity. Everything is working good but my AsyncTask is not waiting for callback method:
public class AsyncOperation extends AsyncTask<String, Integer, String> {
#Override
protected String doInBackground(String... params) {
String linkUrl = params[0];
functionDoStuff(linkUrl);
return "Executed";
}
public void functionDoStuff(String urlLink) {
... code ...
String str = getFile(urlLink);
!!! is not waiting for result !!!
... use 'str' ...
}
private String getFile(String urlLink) {
String savedFileDestination = null;
OkHttpClient client = new OkHttpClient();
final Request request = new Request.Builder()
.url(urlLink")
.build();
client.newCall(request).enqueue(new com.squareup.okhttp.Callback() {
#Override
public void onFailure(Request request, IOException e) {
//something goes wrong
}
#Override
public void onResponse(com.squareup.okhttp.Response response) throws IOException {
//get stream
InputStream inputStream = response.body().byteStream();
//this method save file and return file path
savedFileDestination = saveFileMethod(inputStream);
}
});
return savedFileDestination;
}
}
How can I wait for this callback to continue logic in functiobDoStuff() ?
put all the stuff inside onResponse method.Because onResponse method work asyncronusly
#Selvin is right, I have to make my request synchronous and my 'waiting problem' is gone!
The only change is into getFile() method, it should be smth like:
private void getFile(String urlLink) {
OkHttpClient client = new OkHttpClient();
final Request request = new Request.Builder()
.url(urlLink)
.build();
try {
Response response = client.newCall(request).execute();
if (response!=null){
InputStream inputStream = response.body().byteStream();
saveFile(inputStream);
}
} catch (IOException e) {
e.printStackTrace();
}
}
I'm trying to down/upload a file with retrofit 2 but can't find any tutorials examples on how to do so.
My code for downloading is:
#GET("documents/checkout")
public Call<File> checkout(#Query(value = "documentUrl") String documentUrl, #Query(value = "accessToken") String accessToken, #Query(value = "readOnly") boolean readOnly);
and
Call<File> call = RetrofitSingleton.getInstance(serverAddress)
.checkout(document.getContentUrl(), apiToken, readOnly[i]);
call.enqueue(new Callback<File>() {
#Override
public void onResponse(Response<File> response,
Retrofit retrofit) {
String fileName = document.getFileName();
try {
System.out.println(response.body());
long fileLength = response.body().length();
InputStream input = new FileInputStream(response.body());
File path = Environment.getExternalStorageDirectory();
File file = new File(path, fileName);
BufferedOutputStream output = new BufferedOutputStream(
new FileOutputStream(file));
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
} catch (IOException e) {
String logTag = "TEMPTAG";
Log.e(logTag, "Error while writing file!");
Log.e(logTag, e.toString());
}
}
#Override
public void onFailure(Throwable t) {
// TODO: Error handling
System.out.println(t.toString());
}
});
I've tried with Call and Call but nothing seems to work.
The server-side code writes the file's bytes into HttpServletResponse's output stream after setting the headers and mime type correctly.
What am I doing wrong?
Finally, the upload code:
#Multipart
#POST("documents/checkin")
public Call<String> checkin(#Query(value = "documentId") String documentId, #Query(value = "name") String fileName, #Query(value = "accessToken") String accessToken, #Part("file") RequestBody file);
and
RequestBody requestBody = RequestBody.create(MediaType.parse(document.getMimeType()), file);
Call<String> call = RetrofitSingleton.getInstance(serverAddress).checkin(documentId, document.getFileName(), apiToken, requestBody);
call.enqueue(new Callback<String>() {
#Override
public void onResponse(Response<String> response, Retrofit retrofit) {
System.out.println(response.body());
}
#Override
public void onFailure(Throwable t) {
System.out.println(t.toString());
}
});
Thanks!
Edit:
After the answer, downloading only yields a corrupted file (without the #Streaming), uploading doesn't as well. When I use the above code, the server returns a 400 error. After changing it to
RequestBody requestBody = RequestBody.create(MediaType.parse(document.getMimeType()), file);
MultipartBuilder multipartBuilder = new MultipartBuilder();
multipartBuilder.addFormDataPart("file", document.getFileName(), requestBody);
Call<String> call = RetrofitSingleton.getInstance(serverAddress).checkin(documentId, document.getFileName(), apiToken, multipartBuilder.build());
, the request executes but the backend doesn't seem to receive a file.
Backend code:
#RequestMapping(value = "/documents/checkin", method = RequestMethod.POST)
public void checkInDocument(#RequestParam String documentId,
#RequestParam String name, #RequestParam MultipartFile file,
#RequestParam String accessToken, HttpServletResponse response)
What am I doing wrong? I was able to use the backend from plain Java with the Apache HttpClient:
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("file", new File("E:\\temp\\test.jpg"));
HttpEntity httpEntity = builder.build();
System.out.println("HttpEntity " + EntityUtils.toString(httpEntity.));
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(httpEntity);
Edit v2
For anyone interested, both up- and downloading work now: These are the solutions:
Service:
#GET("documents/checkout")
public Call<ResponseBody> checkout(#Query(value = "documentUrl") String documentUrl, #Query(value = "accessToken") String accessToken, #Query(value = "readOnly") boolean readOnly);
#Multipart
#POST("documents/checkin")
public Call<String> checkin(#Query(value = "documentId") String documentId, #Query(value = "name") String fileName, #Query(value = "accessToken") String accessToken, #Part("file") RequestBody file);
Download Code:
Call<ResponseBody> call = RetrofitSingleton.getInstance(serverAddress)
.checkout(document.getContentUrl(), apiToken, readOnly[i]);
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Response<ResponseBody> response,
Retrofit retrofit) {
String fileName = document.getFileName();
try {
File path = Environment.getExternalStorageDirectory();
File file = new File(path, fileName);
FileOutputStream fileOutputStream = new FileOutputStream(file);
IOUtils.write(response.body().bytes(), fileOutputStream);
} catch (IOException e) {
Log.e(logTag, "Error while writing file!");
Log.e(logTag, e.toString());
}
}
#Override
public void onFailure(Throwable t) {
// TODO: Error handling
System.out.println(t.toString());
}
});
Upload Code:
Call<String> call = RetrofitSingleton
.getInstance(serverAddress).checkin(documentId,
document.getFileName(), apiToken,
multipartBuilder.build());
call.enqueue(new Callback<String>() {
#Override
public void onResponse(Response<String> response,
Retrofit retrofit) {
// Handle response here
}
#Override
public void onFailure(Throwable t) {
// TODO: Error handling
System.out.println("Error");
System.out.println(t.toString());
}
});
For downloading, you can use ResponseBody as your return type --
#GET("documents/checkout")
#Streaming
public Call<ResponseBody> checkout(#Query("documentUrl") String documentUrl, #Query("accessToken") String accessToken, #Query("readOnly") boolean readOnly);
and you can get the ResponseBody input stream in your call back --
Call<ResponseBody> call = RetrofitSingleton.getInstance(serverAddress)
.checkout(document.getContentUrl(), apiToken, readOnly[i]);
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Response<ResponseBody> response,
Retrofit retrofit) {
String fileName = document.getFileName();
try {
InputStream input = response.body().byteStream();
// rest of your code
Your upload looks okay at first glance if you server handles multipart messages correctly. Is it working? If not, can you explain the failure mode? You also might be able to simplify by not making it multipart. Remove the #Multipart annotation and convert #Path to #Body --
#POST("documents/checkin")
public Call<String> checkin(#Query("documentId") String documentId, #Query("name") String fileName, #Query("accessToken") String accessToken, #Body RequestBody file);
I am using retrofit 2.0.0-beta2 and I had an issue uploading image by using multipart request. I solved it by using this answer: https://stackoverflow.com/a/32796626/2915075
The key for me was to use standard POST with MultipartRequestBody instead of #Multipart annotated request.
Here is my code:
Retrofit service class
#POST("photo")
Call<JsonElement> uploadPhoto(#Body RequestBody imageFile, #Query("sessionId"));
Usage from activity, fragment:
RequestBody fileBody = RequestBody.create(MediaType.parse("image/jpeg"), imageFile);
MultipartBuilder multipartBuilder = new MultipartBuilder();
multipartBuilder.addFormDataPart("photo", imageFile.getName(), fileBody);
RequestBody fileRequestBody = multipartBuilder.build();
//call
mRestClient.getRetrofitService().uploadProfilePhoto(fileRequestBody, sessionId);
i have the same problems, and i found a solution to upload files, that described here
Is it possible to show progress bar when upload image via Retrofit 2
Also I had this problem, This is how i try to solve my problem (RETROFIT 2 )
//1. What We Need From Server ( upload.php Script )
public class FromServer {
String result;
}
//2. Which Interface To Communicate Our upload.php Script?
public interface ServerAPI {
#Multipart
#POST("upload.php")//Our Destination PHP Script
Call<List<FromServer>> upload(
#Part("file_name") String file_name,
#Part("file") RequestBody description);
Retrofit retrofit =
new Retrofit.Builder()
.baseUrl("http://192.168.43.135/retro/") // REMEMBER TO END with /
.addConverterFactory(GsonConverterFactory.create())
.build();
}
//3. How To Upload
private void upload(){
ServerAPI api = ServerAPI.retrofit.create(ServerAPI.class);
File from_phone = FileUtils.getFile(Environment.getExternalStorageDirectory()+"/aa.jpg"); //org.apache.commons.io.FileUtils
RequestBody to_server = RequestBody.create(MediaType.parse("multipart/form-data"), from_phone);
api.upload(from_phone.getName(),to_server).enqueue(new Callback<List<FromServer>>() {
#Override
public void onResponse(Call<List<FromServer>> call, Response<List<FromServer>> response) {
Toast.makeText(MainActivity.this, response.body().get(0).result, Toast.LENGTH_SHORT).show();
}
#Override
public void onFailure(Call<List<FromServer>> call, Throwable t) { }
});
}
//4. upload.php
<?php
$pic = $_POST['file_name'];
$pic = str_replace("\"", "", $pic); //REMOVE " from file name
if(file_exists($pic)){unlink($pic);}
$f = fopen($pic, "w");
fwrite($f,$_POST['file']);
fclose($f);
$arr[] = array("result"=>"Done");
print(json_encode($arr));
?>
You can refer tutorial for Image Download using Retrofit 2.0
For the time being you can refer following functions for image download:
void getRetrofitImage() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitImageAPI service = retrofit.create(RetrofitImageAPI.class);
Call<ResponseBody> call = service.getImageDetails();
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Response<ResponseBody> response, Retrofit retrofit) {
try {
Log.d("onResponse", "Response came from server");
boolean FileDownloaded = DownloadImage(response.body());
Log.d("onResponse", "Image is downloaded and saved ? " + FileDownloaded);
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}
#Override
public void onFailure(Throwable t) {
Log.d("onFailure", t.toString());
}
});
}
Following is the file handling part image download using Retrofit 2.0
private boolean DownloadImage(ResponseBody body) {
try {
Log.d("DownloadImage", "Reading and writing file");
InputStream in = null;
FileOutputStream out = null;
try {
in = body.byteStream();
out = new FileOutputStream(getExternalFilesDir(null) + File.separator + "AndroidTutorialPoint.jpg");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
}
catch (IOException e) {
Log.d("DownloadImage",e.toString());
return false;
}
finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
int width, height;
ImageView image = (ImageView) findViewById(R.id.imageViewId);
Bitmap bMap = BitmapFactory.decodeFile(getExternalFilesDir(null) + File.separator + "AndroidTutorialPoint.jpg");
width = 2*bMap.getWidth();
height = 6*bMap.getHeight();
Bitmap bMap2 = Bitmap.createScaledBitmap(bMap, width, height, false);
image.setImageBitmap(bMap2);
return true;
} catch (IOException e) {
Log.d("DownloadImage",e.toString());
return false;
}
}
I hope it will help. All the best. Happy Coding :)