How to form an URL in Background thread before using it with an API?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am working with an API for the first time.



My need is that I need to form an URL with certain parameters out of which one parameter cannot be formed in the Main UI thread and has to be formed in the Background thread.



I am planning to use to volley library to post the GET request.



I am using the Needle library which helps in running background tasks. This is What I have tried till now.



Needle.onBackgroundThread().execute(new UiRelatedTask<String>() {
@Override
protected String doWork() {

return url = GetUrl();
}

@Override
protected void thenDoUiRelatedWork(String result1) {
Log.e("JSON", url);
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener() {
@Override
public void onResponse(Object response) {
Log.e("JSON", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("JSON", error.toString());
}
});
RequestQueue queue = VolleyController.getInstance(Activity.this.getApplicationContext()).getRequestQueue();
queue.start();
queue.add(jsonObjectRequest);
}
});


But the Main problem is that After the GetUrl() method immediately the thenDoUiRelatedWork method is called hence the request is made using an Invalid URL as the URL will still be loading in the background and then when the loading of the URL is over I am logging the URL Which is right.



I cannot use an AysncTask as my app is already using three AsyncTaks and additionally two more could be activated by the user based on the feature he is using. And also the API request has to be done in various places (3-4 places) hence using an AsyncTask will not be suitable.



Can anyone help me to first form the URL fully in the Background and the use volley to post the GET request.










share|improve this question


















  • 1





    You can use an inf loop before creating jsonObjectRequest and wait for valid URL. Something like this while(url != null){;} (btw it is not a good solution at all.)

    – Math10
    Nov 25 '18 at 10:55











  • Thanks for the Answer. Will use this solution in the last if I am not able to settle with any other solution. Thanks.

    – Rajesh K
    Nov 25 '18 at 11:00











  • Can you post your GetUrl function? According to the documentation, thenDoUiRelatedWork is called after finishing execution of doWork. If you end up using @Math10 approach, it would be better to use infinite loop in doWork function, as that runs in background, instead of thenDoUiRelatedWork which will run on Main thread. zsoltsafrany.github.io/needle

    – nkalra0123
    Nov 25 '18 at 12:10













  • Let me know if you use inf loop as solution, I will add it as answer.

    – Math10
    Nov 25 '18 at 13:44











  • @Math10 OK. Will surely do that.

    – Rajesh K
    Nov 25 '18 at 18:35


















0















I am working with an API for the first time.



My need is that I need to form an URL with certain parameters out of which one parameter cannot be formed in the Main UI thread and has to be formed in the Background thread.



I am planning to use to volley library to post the GET request.



I am using the Needle library which helps in running background tasks. This is What I have tried till now.



Needle.onBackgroundThread().execute(new UiRelatedTask<String>() {
@Override
protected String doWork() {

return url = GetUrl();
}

@Override
protected void thenDoUiRelatedWork(String result1) {
Log.e("JSON", url);
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener() {
@Override
public void onResponse(Object response) {
Log.e("JSON", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("JSON", error.toString());
}
});
RequestQueue queue = VolleyController.getInstance(Activity.this.getApplicationContext()).getRequestQueue();
queue.start();
queue.add(jsonObjectRequest);
}
});


But the Main problem is that After the GetUrl() method immediately the thenDoUiRelatedWork method is called hence the request is made using an Invalid URL as the URL will still be loading in the background and then when the loading of the URL is over I am logging the URL Which is right.



I cannot use an AysncTask as my app is already using three AsyncTaks and additionally two more could be activated by the user based on the feature he is using. And also the API request has to be done in various places (3-4 places) hence using an AsyncTask will not be suitable.



Can anyone help me to first form the URL fully in the Background and the use volley to post the GET request.










share|improve this question


















  • 1





    You can use an inf loop before creating jsonObjectRequest and wait for valid URL. Something like this while(url != null){;} (btw it is not a good solution at all.)

    – Math10
    Nov 25 '18 at 10:55











  • Thanks for the Answer. Will use this solution in the last if I am not able to settle with any other solution. Thanks.

    – Rajesh K
    Nov 25 '18 at 11:00











  • Can you post your GetUrl function? According to the documentation, thenDoUiRelatedWork is called after finishing execution of doWork. If you end up using @Math10 approach, it would be better to use infinite loop in doWork function, as that runs in background, instead of thenDoUiRelatedWork which will run on Main thread. zsoltsafrany.github.io/needle

    – nkalra0123
    Nov 25 '18 at 12:10













  • Let me know if you use inf loop as solution, I will add it as answer.

    – Math10
    Nov 25 '18 at 13:44











  • @Math10 OK. Will surely do that.

    – Rajesh K
    Nov 25 '18 at 18:35














0












0








0








I am working with an API for the first time.



My need is that I need to form an URL with certain parameters out of which one parameter cannot be formed in the Main UI thread and has to be formed in the Background thread.



I am planning to use to volley library to post the GET request.



I am using the Needle library which helps in running background tasks. This is What I have tried till now.



Needle.onBackgroundThread().execute(new UiRelatedTask<String>() {
@Override
protected String doWork() {

return url = GetUrl();
}

@Override
protected void thenDoUiRelatedWork(String result1) {
Log.e("JSON", url);
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener() {
@Override
public void onResponse(Object response) {
Log.e("JSON", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("JSON", error.toString());
}
});
RequestQueue queue = VolleyController.getInstance(Activity.this.getApplicationContext()).getRequestQueue();
queue.start();
queue.add(jsonObjectRequest);
}
});


But the Main problem is that After the GetUrl() method immediately the thenDoUiRelatedWork method is called hence the request is made using an Invalid URL as the URL will still be loading in the background and then when the loading of the URL is over I am logging the URL Which is right.



I cannot use an AysncTask as my app is already using three AsyncTaks and additionally two more could be activated by the user based on the feature he is using. And also the API request has to be done in various places (3-4 places) hence using an AsyncTask will not be suitable.



Can anyone help me to first form the URL fully in the Background and the use volley to post the GET request.










share|improve this question














I am working with an API for the first time.



My need is that I need to form an URL with certain parameters out of which one parameter cannot be formed in the Main UI thread and has to be formed in the Background thread.



I am planning to use to volley library to post the GET request.



I am using the Needle library which helps in running background tasks. This is What I have tried till now.



Needle.onBackgroundThread().execute(new UiRelatedTask<String>() {
@Override
protected String doWork() {

return url = GetUrl();
}

@Override
protected void thenDoUiRelatedWork(String result1) {
Log.e("JSON", url);
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener() {
@Override
public void onResponse(Object response) {
Log.e("JSON", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("JSON", error.toString());
}
});
RequestQueue queue = VolleyController.getInstance(Activity.this.getApplicationContext()).getRequestQueue();
queue.start();
queue.add(jsonObjectRequest);
}
});


But the Main problem is that After the GetUrl() method immediately the thenDoUiRelatedWork method is called hence the request is made using an Invalid URL as the URL will still be loading in the background and then when the loading of the URL is over I am logging the URL Which is right.



I cannot use an AysncTask as my app is already using three AsyncTaks and additionally two more could be activated by the user based on the feature he is using. And also the API request has to be done in various places (3-4 places) hence using an AsyncTask will not be suitable.



Can anyone help me to first form the URL fully in the Background and the use volley to post the GET request.







java android api android-asynctask android-volley






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 10:37









Rajesh KRajesh K

5916




5916








  • 1





    You can use an inf loop before creating jsonObjectRequest and wait for valid URL. Something like this while(url != null){;} (btw it is not a good solution at all.)

    – Math10
    Nov 25 '18 at 10:55











  • Thanks for the Answer. Will use this solution in the last if I am not able to settle with any other solution. Thanks.

    – Rajesh K
    Nov 25 '18 at 11:00











  • Can you post your GetUrl function? According to the documentation, thenDoUiRelatedWork is called after finishing execution of doWork. If you end up using @Math10 approach, it would be better to use infinite loop in doWork function, as that runs in background, instead of thenDoUiRelatedWork which will run on Main thread. zsoltsafrany.github.io/needle

    – nkalra0123
    Nov 25 '18 at 12:10













  • Let me know if you use inf loop as solution, I will add it as answer.

    – Math10
    Nov 25 '18 at 13:44











  • @Math10 OK. Will surely do that.

    – Rajesh K
    Nov 25 '18 at 18:35














  • 1





    You can use an inf loop before creating jsonObjectRequest and wait for valid URL. Something like this while(url != null){;} (btw it is not a good solution at all.)

    – Math10
    Nov 25 '18 at 10:55











  • Thanks for the Answer. Will use this solution in the last if I am not able to settle with any other solution. Thanks.

    – Rajesh K
    Nov 25 '18 at 11:00











  • Can you post your GetUrl function? According to the documentation, thenDoUiRelatedWork is called after finishing execution of doWork. If you end up using @Math10 approach, it would be better to use infinite loop in doWork function, as that runs in background, instead of thenDoUiRelatedWork which will run on Main thread. zsoltsafrany.github.io/needle

    – nkalra0123
    Nov 25 '18 at 12:10













  • Let me know if you use inf loop as solution, I will add it as answer.

    – Math10
    Nov 25 '18 at 13:44











  • @Math10 OK. Will surely do that.

    – Rajesh K
    Nov 25 '18 at 18:35








1




1





You can use an inf loop before creating jsonObjectRequest and wait for valid URL. Something like this while(url != null){;} (btw it is not a good solution at all.)

– Math10
Nov 25 '18 at 10:55





You can use an inf loop before creating jsonObjectRequest and wait for valid URL. Something like this while(url != null){;} (btw it is not a good solution at all.)

– Math10
Nov 25 '18 at 10:55













Thanks for the Answer. Will use this solution in the last if I am not able to settle with any other solution. Thanks.

– Rajesh K
Nov 25 '18 at 11:00





Thanks for the Answer. Will use this solution in the last if I am not able to settle with any other solution. Thanks.

– Rajesh K
Nov 25 '18 at 11:00













Can you post your GetUrl function? According to the documentation, thenDoUiRelatedWork is called after finishing execution of doWork. If you end up using @Math10 approach, it would be better to use infinite loop in doWork function, as that runs in background, instead of thenDoUiRelatedWork which will run on Main thread. zsoltsafrany.github.io/needle

– nkalra0123
Nov 25 '18 at 12:10







Can you post your GetUrl function? According to the documentation, thenDoUiRelatedWork is called after finishing execution of doWork. If you end up using @Math10 approach, it would be better to use infinite loop in doWork function, as that runs in background, instead of thenDoUiRelatedWork which will run on Main thread. zsoltsafrany.github.io/needle

– nkalra0123
Nov 25 '18 at 12:10















Let me know if you use inf loop as solution, I will add it as answer.

– Math10
Nov 25 '18 at 13:44





Let me know if you use inf loop as solution, I will add it as answer.

– Math10
Nov 25 '18 at 13:44













@Math10 OK. Will surely do that.

– Rajesh K
Nov 25 '18 at 18:35





@Math10 OK. Will surely do that.

– Rajesh K
Nov 25 '18 at 18:35












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466647%2fhow-to-form-an-url-in-background-thread-before-using-it-with-an-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466647%2fhow-to-form-an-url-in-background-thread-before-using-it-with-an-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud