Does Exoplayer supports playing videos via POST METHOD? - java

I have an API to retrieve videos from our server, the API use the POST method and needs Authorization for Headers and deviceInfo for body parameter.
example.
URL: https://myapi.com/api/pretty_video.mp4
BODY: deviceInfo = device info
HEADER: Authorization: Bearer "Token"
METHOD: POST
I can't find any example of ExoPlayer using POST method in playing videos from URL.

SOLVED!
I solved it by using OkHttp.
DefaultBandwidthMeter defaultBandwidthMeter = new DefaultBandwidthMeter();
DataSource.Factory dataSourceFactory = new OkHttpDataSourceFactory(new OkHttpClient
.Builder()
.connectTimeout(1, TimeUnit.MINUTES)
.readTimeout(1,TimeUnit.MINUTES)
.retryOnConnectionFailure(false)
.addInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
MediaType CONTENT_TYPE = MediaType.parse("application/x-www-form-urlencoded");
RequestBody requestBody = RequestBody.create(CONTENT_TYPE,"deviceinfo=12345");
Request request = chain.request().newBuilder()
.post(requestBody) . // HERE IS THE KEY
.addHeader("Accept", "application/json")
.addHeader("Authorization", "Bearer " + auth)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
return chain.proceed(request);
}
})
.build(), Util.getUserAgent(context,context.getString(R.string.app_name)), defaultBandwidthMeter);

Related

okhttp3 passing parameter with "--data {json}"

How can I pass parameter with "--data {json}" on okHttp3? Do I need to add it in the headers like below? Or its not on the header, it need to be on another object?
Request request = new Request.Builder()
.header("Content-Type", "application/json; charset=utf-8")
.addHeader("data", "{json}")
.url(url)
Please let me know.
You must add to RequestBody :
final String BOUNDARY = String.valueOf(System.currentTimeMillis());
RequestBody requestBody = new MultipartBody.Builder(BOUNDARY)
.setType(MultipartBody.FORM)
.addFormDataPart("data", JsonData)
//.addFormDataPart("otherPara", otherPara)
.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();

JAVA - How to build a client for OAuth 1.0 REST API with signature method as HMAC-SHA256?

I'm able to consume APIs (OAuth 1.0 Authorization & signature method as HMAC-SHA256) in POSTMAN, but not working JAVA (maven project).
Generated code from POSTMAN with OkHttp & Unirest libraries, both are not working. They give the following error.
403 Forbidden
I understand error is related to invalid authentication parameters. But not able to figure out what needs to be changed. Because same keys are working in Postman
OkHttp JAVA Code
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("application/json");
String url = "https://www.example.com?script=508&deploy=1";
String JSONpayload = "{}";
RequestBody body = RequestBody.create(mediaType, JSONpayload);
Request request = new Request.Builder()
.url(url)
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "OAuth realm=\"1111222_SB1\",oauth_consumer_key=\"xxxxxxxxx\",oauth_token=\"xxxxxxxx\",oauth_signature_method=\"HMAC-SHA256\",oauth_timestamp=\"1628747273\",oauth_nonce=\"xxxxxx\",oauth_version=\"1.0\",oauth_signature=\"xxxxxxxxxxx\"")
.addHeader("Cookie", "NS_ROUTING_VERSION=LAGGING")
.build();
Response response = client.newCall(request).execute();
Unirest JAVA Code
Unirest.setTimeouts(0, 0);
String url = "https://www.example.com?script=508&deploy=1";
String JSONpayload = "{}";
HttpResponse<String> response = Unirest.post(url)
.header("Content-Type", "application/json")
.header("Authorization", "OAuth realm=\"1114415_SB1\",oauth_consumer_key=\"xxxxxxxxxx\",oauth_token=\"xxxxxxxxxxx\",oauth_signature_method=\"HMAC-SHA256\",oauth_timestamp=\"1628747273\",oauth_nonce=\"xxxxxxxx\",oauth_version=\"1.0\",oauth_signature=\"xxxxxxxx\"")
.header("Cookie", "NS_ROUTING_VERSION=LAGGING")
.body(JSONpayload).asString();
Any kind of help is appreciated. Thanks in Advance.

How should I put code in Background Thread in Java?

I am using an API which uploads some data to the server, I am deploying this on android application.
I tested APIs using Postman and it works absolutely fine and generated code from postman
login code:
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("username","abc#abc.com")
.addFormDataPart("password","E1234567")
.build();
Request request = new Request.Builder()
.url("192.168.1.51/auth/login")
.method("POST", body)
.addHeader("User-Agent", "Koala Admin")
.addHeader("Content-Type", "application/json")
.addHeader("Cookie", "session=3d9c1888-e9b0-40b3-958b-71c50538d338")
.build();
Response response = client.newCall(request).execute();
create user and upload photo
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("subject_type","1")
.addFormDataPart("name","jasim")
.addFormDataPart("start_time","1619496549")
.addFormDataPart("end_time","1619525349")
.addFormDataPart("photo","/C:/Users/jasim/Pictures/Camera Roll/WIN_20210422_14_54_39_Pro.jpg",
RequestBody.create(MediaType.parse("application/octet-stream"),
new File("/C:/Users/jasim/Pictures/Camera Roll/WIN_20210422_14_54_39_Pro.jpg")))
.build();
Request request = new Request.Builder()
.url("192.168.1.51/subject/file")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Cookie", "session=3d9c1888-e9b0-40b3-958b-71c50538d338")
.build();
Response response = client.newCall(request).execute();
Now I want to know that how can I put these code in AsyncTask class and make the necessary code in background.
You can call enqueue method instead of execute and it will work on the background thread:
client.newCall(request).enqueue(new Callback() {
#Override
public void onResponse(Call call, Response response) throws IOException {
// handle your response
}
#Override
public void onFailure(Call call, IOException e) {
// handle the failure
}
});

Okhttp3 Base URL for calling

I am using an OkHttp Client for my Webservice call, its working successfully with JAVA Main function and returning response.
Now, I want to call this client from Postman; but I dont know what URL Shall i call? Like in jersey we create a base URL from #PATH annotation in Spring we use #service. What shall be done in okhttp to create a base url to call? Please help.
public String soapCaller() throws IOException, JSONException {
Response response = null;
try {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=0146b9a4-7e99-4c83-8e9e-6049cfec55da&client_secret=cJ5nD0yJ4fV8eM1nU4tK2yI5wQ0lG6iE7cP5bD4lQ8dB0jS6pV&scope=ABLApis");
Request request = new Request.Builder()
.url("https://uat-api.abl.com/abl-api/uat/oauth2/token")
.post(body)
.addHeader("content-type", "application/x-www-form-urlencoded")
.addHeader("accept", "application/json")
.build();
response = client.newCall(request).execute();
}
catch(Exception e)
{
logger.info("e: " + e);
}
return response.toString();
}
}
with Jersey Annotations
#GET
#Path("/fundTransfer")
#Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
public String soapCaller() throws IOException, JSONException {
/////////////////////////////
Response response = null;
try {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=0146b9a4-7e99-4c83-8e9e-6049cfec55da&client_secret=cJ5nD0yJ4fV8eM1nU4tK2yI5wQ0lG6iE7cP5bD4lQ8dB0jS6pV&scope=ABLApis");
Request request = new Request.Builder()
.url("https://uat-api.abl.com/abl-api/uat/oauth2/token")
.post(body)
.addHeader("content-type", "application/x-www-form-urlencoded")
.addHeader("accept", "application/json")
.build();
response = client.newCall(request).execute();
}
catch(Exception e)
{
logger.info("e: " + e);
}
///////////////////////////////////////////
return response.toString();
}
}

Authorization header not recognized by Yelp Fusion API

I am passing an authorization header with my Retrofit GET requests as documented by the Yelp Fusion documentation, which is shown below:
Authorization: Bearer MY_API_KEY
I am adding a network interceptor in my onCreate() method as shown below:
OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
okHttpBuilder.addInterceptor(new Interceptor() {
#Override
public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.addHeader("Authorization", "Bearer MY_API_KEY");
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
//Yelp Client init
yelpRetrofit = new Retrofit.Builder()
.baseUrl("https://httpbin.org/")
.client(okHttpBuilder.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
yelpClient = yelpRetrofit.create(YelpClient.class);
Here is my YelpClient class :
public interface YelpClient {
#GET("headers")
Call<YelpJSON> searchTest(); }
This is the response I get when sending the request:
{"error": {"code": "TOKEN_MISSING", "description": "An access token must be supplied in order to use this endpoint."}}
I have successfully retrieved a response from the Yelp Fusion API with Postman and curl both using identical parameters and headers as my Retrofit request, so I can't blame Yelp. I have also attempted statically and dynamically setting the header with no success. Can anyone help me figure out what is wrong here?

Categories