Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8073299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:17:46+00:00 2026-06-05T14:17:46+00:00

I have no idea why, but when I use URLConnection to send a http

  • 0

I have no idea why, but when I use URLConnection to send a http post request to a url, I can get rhe return fine, but when I use it in an if statement, it stops working.
The program compiles and all that, but if I was to use it to send a http request to a webpage containing yes, and I used the code if(answer.toString == "yes") it would not execute the code inside the if part. I know that it is probably to do with line seperators and such, but could anyone point to me how to fix it.

Code used to get the variable answer:

String data="param1=test1&param2=test2";
try{
  URL lUrl=new URL("http://localhost/test.php");
  URLConnection conn=lUrl.openConnection();
  conn.setDoOutput(true);
  OutputStreamWriter writer=new OutputStreamWriter(conn.getOutputStream());
  writer.write(data);
  writer.flush();
  StringBuffer answer=new StringBuffer();
  BufferedReader reader=new BufferedReader(new InputStreamReader(conn.getInputStream()));
  String line;
  while((line=reader.readLine())!=null){
    answer.append(line);
  }
  writer.close();
  reader.close();
  if(answer.toString()=="yes"){
    System.out.print("1");
  }else{
    System.out.print(answer.toString());
  }
}catch(MalformedURLException ex){
  ex.printStackTrace();
}catch(IOException ex){
  ex.printStackTrace();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:17:48+00:00Added an answer on June 5, 2026 at 2:17 pm

    To compare java strings, use the equals method, not ==.

    Because == can only detect if two strings are the same instance, not if they contain the same value.

    So your test should be :

    if ("yes".equals(answer)) {
    

    Note that I write “yes” as recipient of the equals method because writing answer.equals("yes") would cause a NullPointerException when answer is null.
    In this case there is no risk of having answer null but it’s a good habit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have no idea why, but for some reason the top navigation at http://www.wilwaldon.com/crossing/badnav.html
I use listview but prefer the column size to be fixed. Have no idea
I have no idea how to use javascript but I'm running into an issue
I have to use TScrollbar on a TScrollBox(Yes,it seems bad idea,but I have to)
I have some idea of which to use when but the exact usage is
I have no idea why but only the bottom div works the code is:
I have no idea why but the contentherewrap div does not show fully when
I have no idea why this wont work but I have tried the following:
I have no idea how this works or if it is even possible but
Alright so I have no idea how to even begin doing this But basically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.