Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Tolls

Thread: help with reading response from url

  1. #1
    jaikiran1 is offline Member
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Question help with reading response from url

    Hi, can someone please write me a code to read response from Url and save it to database..???

    I was able to retrieve data from url but.am unsure on how to save all the values in database according to their datatypes....

    Example of url would be.like

    Username :jaikiran
    Password ******
    Email
    .
    .
    .
    .

    It would help me a lot if someone can kindly assist me as this is the system test for an interview i am going to attend......Thanx in advance

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: help with reading response from url

    But surely if you don't know how to do this, and they are testing you on this to determine if you are a good fit for a job, then us giving you the answer is not going to do your prospective employer any favours?
    gimbal2 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jaikiran1 is offline Member
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Re: help with reading response from url

    Well that is true if i were to mug up the code and present it there....which i wont be doing,trust me...i would understand it then present it.

    Since you put it that way, can u please provide me with procedure on how to do it atleast? I will write down the code myself and put myself to test

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: help with reading response from url

    You'll have to learn all about JDBC.

    Trail: JDBC(TM) Database Access (The Java™ Tutorials)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jaikiran1 is offline Member
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Re: help with reading response from url

    URL url = new URL("http://localhost:8080/WebApplication1/home");
    HttpURLConnection con= (HttpURLConnection) url.openConnection();

    String data=(String) con.getContent();
    System.out.println("data is "+data);

  6. #6
    jaikiran1 is offline Member
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Re: help with reading response from url

    Quote Originally Posted by jaikiran1 View Post
    URL url = new URL("http://localhost:8080/WebApplication1/home");
    HttpURLConnection con= (HttpURLConnection) url.openConnection();

    String data=(String) con.getContent();
    System.out.println("data is "+data);
    Here i am getting ClassCastException

    Exception is: Exception in thread "main" java.lang.ClassCastException: sun.net.www.protocol.http.HttpURLConnection$HttpIn putStream cannot be cast to java.lang.String
    at javaapplication1.JavaApplication1.main(JavaApplica tion1.java:20)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: help with reading response from url

    Because getContent() does not return a String.
    In your case it is returning an InputStream of some flavour.

    Have a read of the API.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: help with reading response from url

    Quote Originally Posted by jaikiran1 View Post
    URL url = new URL("http://localhost:8080/WebApplication1/home");
    HttpURLConnection con= (HttpURLConnection) url.openConnection();

    String data=(String) con.getContent();
    System.out.println("data is "+data);
    Guide For New Members
    BB Code List - Java Programming Forum - Learn Java Programming

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    jaikiran1 is offline Member
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Re: help with reading response from url

    yup actually getContent() return type is Object .I need to store body of response in String variable so i cast Object type to String type

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: help with reading response from url

    But that Object is not a String, it's an HttpInputStream, so you cannot cast to a String.
    You need to actually read the stream.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Reading A Response From A Web Browses HTTP Request
    By haroldjclements in forum Networking
    Replies: 2
    Last Post: 10-16-2011, 05:52 AM
  2. TreeListener getting no response?
    By ozzyman in forum New To Java
    Replies: 1
    Last Post: 04-28-2011, 07:59 PM
  3. Replies: 0
    Last Post: 04-07-2011, 04:09 PM
  4. Not getting a response for query
    By Kelly in forum JDBC
    Replies: 3
    Last Post: 04-20-2010, 04:41 AM
  5. Replies: 5
    Last Post: 08-21-2009, 12:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •