Retrofit: Making Web Requests to Internal APIsHow to use java.net.URLConnection to fire and handle HTTP requestsapache commons http client efficiencysending binary data via POST on androidAndroid, Java: HTTP POST RequestGetting an exception while using HttpResponse response = client.execute(request);Upload a file from java client to a apache http serverRemove or replace special character in namevaluepair for http post request androidInject HttpClient to get mock response in Java using GUICEHow to unit test Retrofit 2.0 api calls along with EventBus with robospock?Retrofit 2 login post request returns 500 but works well on postman

Should my PhD thesis be submitted under my legal name?

Can somebody explain Brexit in a few child-proof sentences?

Failed to fetch jessie backports repository

Greatest common substring

Everything Bob says is false. How does he get people to trust him?

Using parameter substitution on a Bash array

Is there a good way to store credentials outside of a password manager?

Ways to speed up user implemented RK4

Coordinate position not precise

Can a monster with multiattack use this ability if they are missing a limb?

How can I replace every global instance of "x[2]" with "x_2"

What defines a dissertation?

Best way to store options for panels

Bash method for viewing beginning and end of file

How to be diplomatic in refusing to write code that breaches the privacy of our users

Where in the Bible does the greeting ("Dominus Vobiscum") used at Mass come from?

How do I keep an essay about "feeling flat" from feeling flat?

Why did Kant, Hegel, and Adorno leave some words and phrases in the Greek alphabet?

Displaying the order of the columns of a table

voltage of sounds of mp3files

Was the picture area of a CRT a parallelogram (instead of a true rectangle)?

Cynical novel that describes an America ruled by the media, arms manufacturers, and ethnic figureheads

Is there a problem with hiding "forgot password" until it's needed?

Tiptoe or tiphoof? Adjusting words to better fit fantasy races



Retrofit: Making Web Requests to Internal APIs


How to use java.net.URLConnection to fire and handle HTTP requestsapache commons http client efficiencysending binary data via POST on androidAndroid, Java: HTTP POST RequestGetting an exception while using HttpResponse response = client.execute(request);Upload a file from java client to a apache http serverRemove or replace special character in namevaluepair for http post request androidInject HttpClient to get mock response in Java using GUICEHow to unit test Retrofit 2.0 api calls along with EventBus with robospock?Retrofit 2 login post request returns 500 but works well on postman













0















I want to make a request to my organisation api's. The request contains Headers, UserName, Password, & Cookie for session management.



Below is the actual code (in HttpClient) which I want to rewrite using Retrofit. I have heard that HttpClient libraries have been deprecated or someting so have opted Retrofit. I expect the response with 200 status code.



 public static CookieStore cookingStore = new BasicCookieStore();
public static HttpContext context = new BasicHttpContext();
public String getAuth(String login,String password)
String resp = null;
try
String url = DOMAIN+"myxyzapi/myanything";
context.setAttribute(HttpClientContext.COOKIE_STORE, cookingStore);
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
String log = URLEncoder.encode(login, "UTF-8");
String pass = URLEncoder.encode(password, "UTF-8");
String json = "username="+log+"&password="+pass+"&maintain=true&finish=Go";
StringEntity entity = new StringEntity(json);
post.setEntity(entity);
post.addHeader("Content-Type", "application/x-www-form-urlencoded");
HttpResponse response = client.execute(post,context);
resp = EntityUtils.toString(response.getEntity());
accountPoller();
catch(Exception a)
log.info("Exception in authentication api:"+a.getMessage().toString());

return resp;



Below is my code where I can't figure out how to pass the context with request. HttpResponse response = client.execute(post,**context**); using retrofit.
I don't even know if I have made my retrofit request right.



try 

String log = URLEncoder.encode(login, "UTF-8");
String pass = URLEncoder.encode(password, "UTF-8");

RequestBody formBody = new FormBody.Builder()
.add("username=", xyz)
.add("password=", mypass)
.add("&maintain=", "true")
.add("finish=", "Go")
.build();


String url = www.xyz.com+"myxyzapi/myanything";

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).post(formBody).addHeader("Content-Type", "application/x-www-form-urlencoded").build();
client.newCall(request).enqueue(new Callback()
@Override
public void onFailure(Call call, IOException e)
e.printStackTrace();


@Override
public void onResponse(Call call, Response response) throws IOException
if(response.isSuccessful())
final String myresp = response.body().string();



);

catch(Exception a)
a.getMessage();










share|improve this question









New contributor




khoks 02 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Please avoid using overly descriptive titles. The edit to your title was OK: The second sentence should be something that is added as part of your question instead. A general rule of thumb is that if your title needs to be broken into two or more sentences, it's too long.

    – Hoppeduppeanut
    Mar 22 at 5:28
















0















I want to make a request to my organisation api's. The request contains Headers, UserName, Password, & Cookie for session management.



Below is the actual code (in HttpClient) which I want to rewrite using Retrofit. I have heard that HttpClient libraries have been deprecated or someting so have opted Retrofit. I expect the response with 200 status code.



 public static CookieStore cookingStore = new BasicCookieStore();
public static HttpContext context = new BasicHttpContext();
public String getAuth(String login,String password)
String resp = null;
try
String url = DOMAIN+"myxyzapi/myanything";
context.setAttribute(HttpClientContext.COOKIE_STORE, cookingStore);
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
String log = URLEncoder.encode(login, "UTF-8");
String pass = URLEncoder.encode(password, "UTF-8");
String json = "username="+log+"&password="+pass+"&maintain=true&finish=Go";
StringEntity entity = new StringEntity(json);
post.setEntity(entity);
post.addHeader("Content-Type", "application/x-www-form-urlencoded");
HttpResponse response = client.execute(post,context);
resp = EntityUtils.toString(response.getEntity());
accountPoller();
catch(Exception a)
log.info("Exception in authentication api:"+a.getMessage().toString());

return resp;



Below is my code where I can't figure out how to pass the context with request. HttpResponse response = client.execute(post,**context**); using retrofit.
I don't even know if I have made my retrofit request right.



try 

String log = URLEncoder.encode(login, "UTF-8");
String pass = URLEncoder.encode(password, "UTF-8");

RequestBody formBody = new FormBody.Builder()
.add("username=", xyz)
.add("password=", mypass)
.add("&maintain=", "true")
.add("finish=", "Go")
.build();


String url = www.xyz.com+"myxyzapi/myanything";

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).post(formBody).addHeader("Content-Type", "application/x-www-form-urlencoded").build();
client.newCall(request).enqueue(new Callback()
@Override
public void onFailure(Call call, IOException e)
e.printStackTrace();


@Override
public void onResponse(Call call, Response response) throws IOException
if(response.isSuccessful())
final String myresp = response.body().string();



);

catch(Exception a)
a.getMessage();










share|improve this question









New contributor




khoks 02 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Please avoid using overly descriptive titles. The edit to your title was OK: The second sentence should be something that is added as part of your question instead. A general rule of thumb is that if your title needs to be broken into two or more sentences, it's too long.

    – Hoppeduppeanut
    Mar 22 at 5:28














0












0








0








I want to make a request to my organisation api's. The request contains Headers, UserName, Password, & Cookie for session management.



Below is the actual code (in HttpClient) which I want to rewrite using Retrofit. I have heard that HttpClient libraries have been deprecated or someting so have opted Retrofit. I expect the response with 200 status code.



 public static CookieStore cookingStore = new BasicCookieStore();
public static HttpContext context = new BasicHttpContext();
public String getAuth(String login,String password)
String resp = null;
try
String url = DOMAIN+"myxyzapi/myanything";
context.setAttribute(HttpClientContext.COOKIE_STORE, cookingStore);
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
String log = URLEncoder.encode(login, "UTF-8");
String pass = URLEncoder.encode(password, "UTF-8");
String json = "username="+log+"&password="+pass+"&maintain=true&finish=Go";
StringEntity entity = new StringEntity(json);
post.setEntity(entity);
post.addHeader("Content-Type", "application/x-www-form-urlencoded");
HttpResponse response = client.execute(post,context);
resp = EntityUtils.toString(response.getEntity());
accountPoller();
catch(Exception a)
log.info("Exception in authentication api:"+a.getMessage().toString());

return resp;



Below is my code where I can't figure out how to pass the context with request. HttpResponse response = client.execute(post,**context**); using retrofit.
I don't even know if I have made my retrofit request right.



try 

String log = URLEncoder.encode(login, "UTF-8");
String pass = URLEncoder.encode(password, "UTF-8");

RequestBody formBody = new FormBody.Builder()
.add("username=", xyz)
.add("password=", mypass)
.add("&maintain=", "true")
.add("finish=", "Go")
.build();


String url = www.xyz.com+"myxyzapi/myanything";

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).post(formBody).addHeader("Content-Type", "application/x-www-form-urlencoded").build();
client.newCall(request).enqueue(new Callback()
@Override
public void onFailure(Call call, IOException e)
e.printStackTrace();


@Override
public void onResponse(Call call, Response response) throws IOException
if(response.isSuccessful())
final String myresp = response.body().string();



);

catch(Exception a)
a.getMessage();










share|improve this question









New contributor




khoks 02 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I want to make a request to my organisation api's. The request contains Headers, UserName, Password, & Cookie for session management.



Below is the actual code (in HttpClient) which I want to rewrite using Retrofit. I have heard that HttpClient libraries have been deprecated or someting so have opted Retrofit. I expect the response with 200 status code.



 public static CookieStore cookingStore = new BasicCookieStore();
public static HttpContext context = new BasicHttpContext();
public String getAuth(String login,String password)
String resp = null;
try
String url = DOMAIN+"myxyzapi/myanything";
context.setAttribute(HttpClientContext.COOKIE_STORE, cookingStore);
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
String log = URLEncoder.encode(login, "UTF-8");
String pass = URLEncoder.encode(password, "UTF-8");
String json = "username="+log+"&password="+pass+"&maintain=true&finish=Go";
StringEntity entity = new StringEntity(json);
post.setEntity(entity);
post.addHeader("Content-Type", "application/x-www-form-urlencoded");
HttpResponse response = client.execute(post,context);
resp = EntityUtils.toString(response.getEntity());
accountPoller();
catch(Exception a)
log.info("Exception in authentication api:"+a.getMessage().toString());

return resp;



Below is my code where I can't figure out how to pass the context with request. HttpResponse response = client.execute(post,**context**); using retrofit.
I don't even know if I have made my retrofit request right.



try 

String log = URLEncoder.encode(login, "UTF-8");
String pass = URLEncoder.encode(password, "UTF-8");

RequestBody formBody = new FormBody.Builder()
.add("username=", xyz)
.add("password=", mypass)
.add("&maintain=", "true")
.add("finish=", "Go")
.build();


String url = www.xyz.com+"myxyzapi/myanything";

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).post(formBody).addHeader("Content-Type", "application/x-www-form-urlencoded").build();
client.newCall(request).enqueue(new Callback()
@Override
public void onFailure(Call call, IOException e)
e.printStackTrace();


@Override
public void onResponse(Call call, Response response) throws IOException
if(response.isSuccessful())
final String myresp = response.body().string();



);

catch(Exception a)
a.getMessage();







java android






share|improve this question









New contributor




khoks 02 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




khoks 02 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Mar 22 at 8:09









Hoppeduppeanut

489713




489713






New contributor




khoks 02 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Mar 21 at 15:14









khoks 02khoks 02

13




13




New contributor




khoks 02 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





khoks 02 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






khoks 02 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Please avoid using overly descriptive titles. The edit to your title was OK: The second sentence should be something that is added as part of your question instead. A general rule of thumb is that if your title needs to be broken into two or more sentences, it's too long.

    – Hoppeduppeanut
    Mar 22 at 5:28


















  • Please avoid using overly descriptive titles. The edit to your title was OK: The second sentence should be something that is added as part of your question instead. A general rule of thumb is that if your title needs to be broken into two or more sentences, it's too long.

    – Hoppeduppeanut
    Mar 22 at 5:28

















Please avoid using overly descriptive titles. The edit to your title was OK: The second sentence should be something that is added as part of your question instead. A general rule of thumb is that if your title needs to be broken into two or more sentences, it's too long.

– Hoppeduppeanut
Mar 22 at 5:28






Please avoid using overly descriptive titles. The edit to your title was OK: The second sentence should be something that is added as part of your question instead. A general rule of thumb is that if your title needs to be broken into two or more sentences, it's too long.

– Hoppeduppeanut
Mar 22 at 5:28













1 Answer
1






active

oldest

votes


















0














You have to catch exception and use this class.
retrofit2.HttpException



retrofit2
Class HttpException



int
code()
HTTP status code.
String
message()
HTTP status message.
Response
response()
The full HTTP response.






share|improve this answer























  • the problem is that it is not returning 200 success code. Can you help me achieve that.

    – khoks 02
    Mar 22 at 5:27











  • you have to send me full code with credentials..If you can make it in postman and send me screenshot of all what is added to request . I can convert it to retrofit .

    – Jin Thakur
    Mar 22 at 23:07











  • thanks for timely responses. Anyways the problem has been solved now. Just made some silly mistakes.

    – khoks 02
    Mar 23 at 11:52










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
);



);






khoks 02 is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55283671%2fretrofit-making-web-requests-to-internal-apis%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You have to catch exception and use this class.
retrofit2.HttpException



retrofit2
Class HttpException



int
code()
HTTP status code.
String
message()
HTTP status message.
Response
response()
The full HTTP response.






share|improve this answer























  • the problem is that it is not returning 200 success code. Can you help me achieve that.

    – khoks 02
    Mar 22 at 5:27











  • you have to send me full code with credentials..If you can make it in postman and send me screenshot of all what is added to request . I can convert it to retrofit .

    – Jin Thakur
    Mar 22 at 23:07











  • thanks for timely responses. Anyways the problem has been solved now. Just made some silly mistakes.

    – khoks 02
    Mar 23 at 11:52















0














You have to catch exception and use this class.
retrofit2.HttpException



retrofit2
Class HttpException



int
code()
HTTP status code.
String
message()
HTTP status message.
Response
response()
The full HTTP response.






share|improve this answer























  • the problem is that it is not returning 200 success code. Can you help me achieve that.

    – khoks 02
    Mar 22 at 5:27











  • you have to send me full code with credentials..If you can make it in postman and send me screenshot of all what is added to request . I can convert it to retrofit .

    – Jin Thakur
    Mar 22 at 23:07











  • thanks for timely responses. Anyways the problem has been solved now. Just made some silly mistakes.

    – khoks 02
    Mar 23 at 11:52













0












0








0







You have to catch exception and use this class.
retrofit2.HttpException



retrofit2
Class HttpException



int
code()
HTTP status code.
String
message()
HTTP status message.
Response
response()
The full HTTP response.






share|improve this answer













You have to catch exception and use this class.
retrofit2.HttpException



retrofit2
Class HttpException



int
code()
HTTP status code.
String
message()
HTTP status message.
Response
response()
The full HTTP response.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 21 at 16:32









Jin ThakurJin Thakur

71767




71767












  • the problem is that it is not returning 200 success code. Can you help me achieve that.

    – khoks 02
    Mar 22 at 5:27











  • you have to send me full code with credentials..If you can make it in postman and send me screenshot of all what is added to request . I can convert it to retrofit .

    – Jin Thakur
    Mar 22 at 23:07











  • thanks for timely responses. Anyways the problem has been solved now. Just made some silly mistakes.

    – khoks 02
    Mar 23 at 11:52

















  • the problem is that it is not returning 200 success code. Can you help me achieve that.

    – khoks 02
    Mar 22 at 5:27











  • you have to send me full code with credentials..If you can make it in postman and send me screenshot of all what is added to request . I can convert it to retrofit .

    – Jin Thakur
    Mar 22 at 23:07











  • thanks for timely responses. Anyways the problem has been solved now. Just made some silly mistakes.

    – khoks 02
    Mar 23 at 11:52
















the problem is that it is not returning 200 success code. Can you help me achieve that.

– khoks 02
Mar 22 at 5:27





the problem is that it is not returning 200 success code. Can you help me achieve that.

– khoks 02
Mar 22 at 5:27













you have to send me full code with credentials..If you can make it in postman and send me screenshot of all what is added to request . I can convert it to retrofit .

– Jin Thakur
Mar 22 at 23:07





you have to send me full code with credentials..If you can make it in postman and send me screenshot of all what is added to request . I can convert it to retrofit .

– Jin Thakur
Mar 22 at 23:07













thanks for timely responses. Anyways the problem has been solved now. Just made some silly mistakes.

– khoks 02
Mar 23 at 11:52





thanks for timely responses. Anyways the problem has been solved now. Just made some silly mistakes.

– khoks 02
Mar 23 at 11:52












khoks 02 is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















khoks 02 is a new contributor. Be nice, and check out our Code of Conduct.












khoks 02 is a new contributor. Be nice, and check out our Code of Conduct.











khoks 02 is a new contributor. Be nice, and check out our Code of Conduct.














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%2f55283671%2fretrofit-making-web-requests-to-internal-apis%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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript