dcsimg
Urgent Please - How can i convert a object to a string
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Junaid_Mohammad
Posted On:   Thursday, July 18, 2002 02:07 PM

I have written the following code private String getDateUsingHttpObject() { try { // Construct a URL referring to the servlet URL url = new URL(getCodeBase(), "/servlet/loginServlet"); HttpMessage msg = new HttpMessage(url); Don't reall need, but i don't know how to get rid off it. // Construct a Properties list to say format=object Properties props = new Properties(); props.put("UserID", "UserID"); // Send a GET message to the servlet, passing "props" as a query string // Get the response as an ObjectInputStream    More>>

I have written the following code


private String getDateUsingHttpObject()

{

try

{

// Construct a URL referring to the servlet

URL url = new URL(getCodeBase(), "/servlet/loginServlet");



HttpMessage msg = new HttpMessage(url);



Don't reall need, but i don't know how to get rid off it.


// Construct a Properties list to say format=object

Properties props = new Properties();

props.put("UserID", "UserID");


// Send a GET message to the servlet, passing "props" as a query string

// Get the response as an ObjectInputStream

InputStream in = msg.sendGetMessage(props);

ObjectInputStream result = new ObjectInputStream(in);


// Read the userID object from the stream

Object obj = result.readObject();

userID = obj;


// Return the string representation of the userID
return userID.toString();

}

catch (Exception e)

{

// If there was a problem, print to System.out

// (typically the Java console) and return null

e.printStackTrace();

return null;

}

}


**********

The problem is here,

incompatible types

found : java.lang.Object

required: java.lang.String userID = obj;


I am passing a String

ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());

out.writeObject(UserID);



As follows, no Object, but how can i get the UserID, with having a incompatible types problem.

Please help

Junaid

   <<Less

Re: Urgent Please - How can i convert a object to a string

Posted By:   Jason_Spittel  
Posted On:   Thursday, July 18, 2002 03:40 PM

Hi,
you should be able to simply downcast it (catch for ClassCastException just in case).




NOTE: if you are unfamiliar with log4j, head over to the jakarta site and read up, or you can just replace all the cat.error and cat.debug with System.out.println.





try{

  String userId = (String)result.readObject();

}

catch(ClassNotFoundException cnfe){

//assuming you're using log4j and have a logger by

//the name of cat

  cat.error("Not receiving a String back from the servlet",
cnfe);

}



this should work.



you could also try debugging your app using a bit o Reflection.



Object obj = result.readObject();

if(obj instanceof String){

  cat.debug("Success! yay!");

}

else{

  cat.debug("what the heck is this object anyways?" +   
obj.getClass().toString());
}


Cheers,



J
About | Sitemap | Contact