简体   繁体   中英

Unable to Override toString for Object

This is the code I wrote in Java and accessing Bugzilla and getting the object. But I am unable to get object Values. I am trying to override toString() function but overrided one is not working.

package XMLRPC; 

import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.HashMap; 
import java.util.Map; 

import org.apache.commons.httpclient.HttpClient; 
import org.apache.xmlrpc.XmlRpcException; 
import org.apache.xmlrpc.client.XmlRpcClient; 
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; 
import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory; 

public class Search { 

    @Override 
    public String toString() { 
               //System.err.println ("Ojesh"); 
               return String.format("oje"+"abx"); 
    } 

    public static void main(String args[]) 
          throws MalformedURLException, XmlRpcException { 


               HttpClient httpClient = new HttpClient(); 
            XmlRpcClient rpcClient = new XmlRpcClient(); 
            XmlRpcCommonsTransportFactory factory = new XmlRpcCommonsTransportFactory(rpcClient); 
            XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); 

          factory.setHttpClient(httpClient); 
            rpcClient.setTransportFactory(factory); 
            config.setServerURL(new URL("http://abc2/bugzilla/xmlrpc.cgi")); 
            rpcClient.setConfig(config); 

            //map of the login data 
            Map loginMap = new HashMap(); 
            loginMap.put("login", "abc@bag"); 
            loginMap.put("password", "***"); 
            loginMap.put("rememberlogin", "Bugzilla_remember"); 


            // login to bugzilla 
            Object loginResult = rpcClient.execute("User.login", new Object[]{loginMap}); 
            System.err.println ("loginResult=" + loginResult); 

            // map of the bug data ok 
            Map bugMap = new HashMap(); 

            bugMap.put("id", "350"); 

            //bugMap.put("status", "NEW"); 

            // create bug 
            Object createResult = rpcClient.execute("Bug.search", new Object[]{bugMap}); 
            //createResult.toString(); 

            System.err.println("createResult =" + createResult.toString());
         }
     } 

It's not returning OJEABX as expected. Instead bugs=[Ljava.lang.Object;@2ee5e48a being displayed. Where am I going wrong? I want to print value of Object but not working fine.

You indeed created overridden version of toString() in your Search class. But you do not use it.

Try System.out.println(new Search().toString())); and see how your toString() is working. I do not understand which code line does not work from your point of view.

There are 2 lines that print something:

System.err.println ("loginResult=" + loginResult);

and

System.err.println("createResult =" + createResult.toString());

The first line prints array because loginResult is and array. The second line prints object returned by your rpcClient that in fact is array too. So, you see the toString implementation of array that cannot be changed.

If you want to create string representation of array use Arrays.toString(arr) .

First, you are using String.format which is used to format strings bases on a format as the name implies. See http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

What you need is to use StringBuilder to create the string that you want by appending them and give it to toString()

ex: http://docs.oracle.com/javase/tutorial/java/data/buffers.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM