Executing okhttp post requests one after another - java

I have 3 recursion methods which make post requests by okhttp
The first one is:
private void sendStatPhoto() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (!mSharedPreferences.getString("Statistics_photo", "").equals("")) {
mStatisticsPhoto = mSharedPreferences.getString("Statistics_photo", "").split(";");
System.out.println(mStatisticsPhoto[0]);
mPhoto = DBReader.read(mSQLiteDatabase,
"photo_statistics_" + mStatisticsPhoto[0],
"names");
mDictionaryForRequest = new Hashtable();
mDictionaryForRequest.put("login_admin", QuestionnaireActivity.this.getString(R.string.login_admin));
OkHttpClient client = new OkHttpClient();
client.newCall(new DoRequest(QuestionnaireActivity.this).Post(mDictionaryForRequest, QuestionnaireActivity.this.getString(R.string.url), mPhoto))
.enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
System.out.println("Ошибка");
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(QuestionnaireActivity.this, "Ошибка отправки!", Toast.LENGTH_SHORT).show();
Log.d("TAG", "3 error");
}
});
}
#Override
public void onResponse(Call call, final Response response) throws IOException {
String responseCallback = response.body().string();
if (responseCallback.substring(1, responseCallback.length() - 1).equals("1")) {
mSQLiteDatabase.execSQL("DROP TABLE if exists " + "photo_statistics_" + mStatisticsPhoto[0]);
SharedPreferences.Editor editor = mSharedPreferences.edit()
.putString("Statistics_photo", mSharedPreferences.getString("Statistics_photo", "").replace(mStatisticsPhoto[0] + ";", "")); //temp-оставшиеся анкеты.
editor.apply();
new File(getFilesDir(), "files/" + mPhoto + ".jpg").delete();
System.out.println("Deleted");
Log.d("TAG", "3 good");
sendStatPhoto();
}
}
}
);
}
}
});
}
And 2 other methods which make exactly the same with some other kind of data
private void sendAudio() {...}
private void send() {...}
I need to execute them one after another.
I tried to make the array of methods
Runnable[] methods = new Runnable[]{
new Runnable() {
#Override
public void run() {
Log.d("TAG", "1");
send();
}
},
new Runnable() {
#Override
public void run() {
Log.d("TAG", "2");
sendAudio();
}
},
new Runnable() {
#Override
public void run() {
Log.d("TAG", "3");
sendStatPhoto();
}
}
};
But okhttp makes all posts in new Threads and it does not work.
if (Internet.hasConnection(this)) {
Log.d("TAG", "start");
ExecutorService service = Executors.newSingleThreadExecutor();
for (Runnable r : methods)
service.submit(r);
service.shutdown();
Log.d("TAG", "finish");
}
How to execute one post after another?
The problem is that i want to send a pack of data(like 10-20 times) in 3 metods(in sequence 1-2-3) but i get the data for 3-rd some later and if i ll put the execution of the next method in onResponse i ll lose the 3-rd

use .then to concat http methods:
$http.post(UserPass, {username : vm.data.username, password : vm.data.password
}).then(function (res){
var serverresponse = res.data;
}).then(function (){
// another http request here and so on
$http.post({}).then(function (){
//etc...
});
});
;)
Hope this helps, for me this was a nightmare the first time!
(".then" waits for the http to execute and then proceeds with what it has inside)

Related

How to auto refresh data in android studio every second?

I am new to android studio, I am making an app that gets the json of a field in thingspeak, but I don't know how can I make an auto refresh to the data I get every second. Can you please help me?
public class MainActivity extends AppCompatActivity {
private TextView mTextViewResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextViewResult = findViewById(R.id.text_view_result);
OkHttpClient client = new OkHttpClient();
// Read field url from thingspeak
String url = "https://api.thingspeak.com/channels/XXXXXXX/fields/1.json?api_key=XXXXXXXXXXXXX&results=2";
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onResponse(#NonNull okhttp3.Call call, #NonNull Response response) throws IOException {
if (response.isSuccessful()) {
// this is what I want to refresh every second
final String myResponse = response.body().string();
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
mTextViewResult.setText(myResponse);
}
});
}
}
});
}
}
I found a solution finally, I placed the data request inside the following code and it worked perfectly:
final Handler handler = new Handler();
Runnable refresh = new Runnable() {
#Override
public void run() {
// data request
handler.postDelayed(this, 5000);
}
};
handler.postDelayed(refresh, 5000);
Use a CountDown Timer . Set the timer for 1 second and in onFinish method call request data .
new CountDownTimer(1000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
//Request Data
}
}.start();

Why value is assigned after usage in method?

So, I was bothering with this issue.
I have in my code value named
public String currentSong = "artist- title";
and I use it in code like this
private MetadataOutput getSongTime() {
return new MetadataOutput() {
#Override
public void onMetadata(Metadata metadata) {
final int length = metadata.length();
if (length > 0) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(existingURL.split("play")[0] + "currentsong").build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(#NotNull Call call, #NotNull IOException e) {
call.cancel();
}
#Override
public void onResponse(Call call, Response response) throws IOException {
currentSong = response.body().toString();
}
});
Toast.makeText(RadioService.this, currentSong, Toast.LENGTH_SHORT).show();
}
}
};
}
But value in Toast is always behind the change (on second run it have value I expected on first run)
Is there any way to make it work correctly?
currentSong get the value on the onResponse, and its asynchronous
You must wait the response to show the Toast Message
#Override
public void onResponse(Call call, Response response) throws IOException {
currentSong = response.body().toString();
// you new a new Thread to show info in the screen beacausre the methos is asynchronous
Activity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
// show toast here
}
}
}

android how to make validation url available from retrofit

To check whether youtube url is available I used retrofit, it works well but I want to use it as validation I got stuck, here's my code :
CheckUrlHelper.java
private static final String BASE_URL= "https://youtu.be/";
public void CheckUrl(String youtube_id)
{
retrofit=new Retrofit.Builder().baseUrl(BASE_URL+youtube_id).build();
Call<ResponseBody> call= retrofit.create(CheckUrlAvailable.class).checkUrl();
call.enqueue(new Callback<ResponseBody>()
{
#Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
//Log.d("","response : "+response);
if(response.code() == 200)
{
result = "true";
}
else
{
result = "false";
}
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
result = "false";
}
});
listener.getResult(result);
}
public interface callbackListener {
void getResult(String result);
}
I've tried using interface like code on above but caused error.
My expectation : to get result (if it possibly to get result from code whether 200, 500 or etc), so :
MyClass.java
private String error_message;
public void onClick(View v) {
String video_link = editVideoLink.getText().toString();
CheckUrlHelper curl = new CheckUrlHelper();
/* MY Expectation */
if(curl.CheckUrl(video_link) == 200){
Toast.makeText(getActivity(),"url available",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(),"url not available",Toast.LENGTH_SHORT).show();
}
/**** Edit ****/
String video_link = editVideoLink.getText().toString();
if(TextUtils.is_empty(video_link){
error_message = "Link cannot be empty";
} else if(/* methodNamewhatever(video_link) == false*/ OR /* methodNamewhatever(video_link) == 404*/){
error_message = "Invalid Link";
}else{
// save
}
Toast.makeText(getActivity(),error_message,Toast.LENGTH_SHORT).show();
So when user put youtube id on editText and then click on submit, it could be give the result as validation.
please help me
Thanks
Well, you misplaced the listener's location. Instead of calling it out of all functions, you need to call it once in each method in order to get the result after the response is triggered in onResponse and onFailure callbacks.
So you have to change your code to be like this
private static final String BASE_URL= "https://youtu.be/";
public void CheckUrl(String youtube_id, callbackListener listener)
{
retrofit = new Retrofit.Builder().baseUrl(BASE_URL+youtube_id).build();
Boolean result = false;
Call<ResponseBody> call= retrofit.create(CheckUrlAvailable.class).checkUrl();
call.enqueue(new Callback<ResponseBody>()
{
#Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
//Log.d("","response : "+response);
if(response.code() == 200)
{
result = true;
}
else
{
result = false;
}
listener.getResult(result);
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
result = false;
listener.getResult(result);
}
});
}
public interface callbackListener {
void getResult(Boolean result);
}
I have created some code snippets for you just try to use to solve your issues.
CheckUrlHelper.java
private static final String BASE_URL= "https://youtu.be/";
public void CheckUrl(String youtube_id, callbackListener listener)
{
retrofit = new Retrofit.Builder().baseUrl(BASE_URL+youtube_id).build();
Boolean result = false;
Call<ResponseBody> call= retrofit.create(CheckUrlAvailable.class).checkUrl();
call.enqueue(new Callback<ResponseBody>()
{
#Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
//Log.d("","response : "+response);
if(response.code() == 200)
{
result = true;
}
else
{
result = false;
}
listener.getResult(result);
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
result = false;
listener.getResult(result);
}
});
}
public interface callbackListener {
void getResult(Boolean result);
}
And modify your click code in
MyClass.java
public void onClick(View v) {
String video_link = editVideoLink.getText().toString();
CheckUrlHelper curl = new CheckUrlHelper();
//curl.CheckUrl(video_link);
curl.CheckUrl(video_link, new callbackListener() {
#Override
public void getResult(Boolean result) {
if(result){
Toast.makeText(getActivity(),"url available",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(),"url not available",Toast.LENGTH_SHORT).show();
}
}
});
Hopefully this will solve your problems.

Firebase ML kit's QR Code Scanner Scans each QR Code Multiple Times

My scanner scans single QR Code multiple times thats why my createDialog method runs multiple time where i get the info regarding QR code and the user who is using it and the agent who posted it and store data into users node in Db and because it run multiple time my Db cant keep track of the no. of times the qr code scanned for each user..
private void setupCamera() {
startAgain.setEnabled(isDetected);
startAgain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isDetected = !isDetected;
}
});
cameraView.setLifecycleOwner(this);
cameraView.addFrameProcessor(new FrameProcessor() {
#Override
public void process(#NonNull Frame frame) {
processorImage((FirebaseVisionImage) getVisionImageFromFrame(frame));
}
});
options = new FirebaseVisionBarcodeDetectorOptions.Builder()
.setBarcodeFormats(FirebaseVisionBarcode.FORMAT_QR_CODE)
.build();
detector = FirebaseVision.getInstance().getVisionBarcodeDetector(options);
}
private Object getVisionImageFromFrame(Frame frame) {
byte[] data = frame.getData();
FirebaseVisionImageMetadata metadata = new FirebaseVisionImageMetadata.Builder()
.setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
.setHeight(frame.getSize().getHeight())
.setWidth(frame.getSize().getWidth())
.build();
return FirebaseVisionImage.fromByteArray(data, metadata);
}
private void processorImage(FirebaseVisionImage image) {
if (!isDetected) {
detector.detectInImage(image)
.addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionBarcode>>() {
#Override
public void onSuccess(List<FirebaseVisionBarcode> firebaseVisionBarcodes) {
processResult(firebaseVisionBarcodes);
}
}).addOnCompleteListener(new OnCompleteListener<List<FirebaseVisionBarcode>>() {
#Override
public void onComplete(#NonNull Task<List<FirebaseVisionBarcode>> task) {
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(StoreScanQR.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
private void processResult(List<FirebaseVisionBarcode> firebaseVisionBarcodes) {
if (firebaseVisionBarcodes.size() > 0) {
isDetected = true;
startAgain.setEnabled(isDetected);
for (FirebaseVisionBarcode item : firebaseVisionBarcodes) {
try {
createDialog(item.getRawValue());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
Detection is an asynchronous call, so it may be triggered multiple times with different inputs before you can get the first result. If you only care about the first detected result, you can check your isDetected flag at the result processing side (i.e. in #onSuccess callback) rather than detection triggering side.
#Override
public void onSuccess(List<FirebaseVisionBarcode> firebaseVisionBarcodes) {
if (!isDetected) {
processResult(firebaseVisionBarcodes);
}
}

how to make volley onResponse run in UnitTest thread

i'm making unit test for my application
my unit test class has this method
#Before
public void initialize() {
mContext = InstrumentationRegistry.getTargetContext();
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(mContext).name("realmTest").inMemory().build();
Realm.setDefaultConfiguration(realmConfiguration);
mWorkoutsModel = new WorkoutsModel(mContext);
mRealm = Realm.getInstance(realmConfiguration);
mWorkoutsModel.registerListener(this);
}
#Test
public void getWorkouts() throws Exception {
mWorkoutsModel.onStart();
mLock.await();
mWorkoutsModel.onStop();
}
#After
public void deInitialize() {
mWorkoutsModel.unRegisterListener();
mRealm.close();
}
and my model
#Override
public void onStart() {
mRealm = Realm.getDefaultInstance();
getDataFromApi();
}
private boolean getDataFromApi() {
Constants.AllAPIs.ALLWorkouts allWorkouts = new Constants.AllAPIs.ALLWorkouts();
if (Permissions.isInternetConnectionExist(mContext)) {
mApiHandler.downLoadDataFromApi(AllWorkouts.class, allWorkouts.getBaseUrl(),
new APIHandler.StringResponseHandler<AllWorkouts>() {
#Override
public void onResponse(AllWorkouts response) {
insertWorkouts(response.getWorkouts());
},
new APIHandler.ErrorResponseHandler() {
#Override
public void onErrorResponse(VolleyError error) {
}
}, TAG);
return true;
} else {
return false;
}
}
private void insertWorkouts(final List<Workout> workouts) {
mCurrentInsertTransaction = mRealm.executeTransactionAsync(new Realm.Transaction() {
#Override
public void execute(Realm bgRealm) {
bgRealm.copyToRealmOrUpdate(workouts);
}
}, new Realm.Transaction.OnSuccess() {
#Override
public void onSuccess() {
}
});
}
my problem that the unittest calls onStart which create realm object in the model in test thread but volley force onResponse to run on UIThread which makes realm throw exception Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
the code runs perfect in normal, but in test it fails
does anyone faced same problem or can solve it ?
i solved my problem by run the test in handler
new Handler(mContext.getMainLooper()).post(new Runnable() {
#Override
public void run() {
try {
mWorkoutsModel.onStart();
mLock.await();
mWorkoutsModel.onStop();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
hope that help somebody

Categories