2012-01-12 77 views
0

我試圖做一個xml rpc請求時出現問題。我serive崩潰蒙山的logcat的話說lang.ClassCastException:java.lang.Integer中在我的代碼coresponds到客戶端調用* >>>文字=(字符串)行35 client.call < < < < < *lang.ClassCastException:java.lang.Integer android xml rpc

package tfe.rma.ciss.be; 

import java.net.InetAddress; 
import java.net.NetworkInterface; 
import java.net.SocketException; 
import java.net.URI; 
import java.util.Enumeration; 

import org.xmlrpc.android.XMLRPCClient; 
import org.xmlrpc.android.XMLRPCException; 

import android.app.Service; 
import android.content.Intent; 
import android.net.wifi.WifiInfo; 
import android.net.wifi.WifiManager; 
import android.os.IBinder; 
import android.widget.Toast; 

public class Addviewer extends Service { 

    private XMLRPCClient client; 
    private URI uri; 
    String text="", IpAdress; 

    @Override 
    public void onCreate(){} 

    public void onStart(Intent intent, int StartId){ 

     uri = URI.create("http://fuseng.elte.rma.ac.be:8080/RPC2"); 
      client = new XMLRPCClient(uri); 

      getLocalIpAddress(); 

     if (!IpAdress.equals("R.A.S")){ 

      try { 
      text = (String) client.call("mission.addViewer",IpAdress+ ":" + 8214, "newImage"); 
     } catch (XMLRPCException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      text= "erro in adding viewer with the exception:" + e + "/n" + "try again later"; 
     } 
      Toast.makeText(this,"suscribtion to the viewer with the result " + text, Toast.LENGTH_SHORT).show(); } 


     else { 
      Toast.makeText(this,"No network avalaible right now" + "/n" + "try again later", Toast.LENGTH_SHORT).show(); 

     } 

    } 

    public String getLocalIpAddress() { 
     try { 
      for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { 
       NetworkInterface intf = en.nextElement(); 
       for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { 
        InetAddress inetAddress = enumIpAddr.nextElement(); 
        if (!inetAddress.isLoopbackAddress()) { 
         IpAdress= inetAddress.getHostAddress().toString(); 
         return inetAddress.getHostAddress().toString(); 
        } 
       } 
      } 
     } catch (SocketException ex) { 
      IpAdress="R.A.S"; 
     } 
     return null; 
    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 



} 

,所以我用一個整數(即214)改變了sparametr newImage,然後將其(服務)工作正常,只是服務器回覆我說,它期待在第二個參數字符串(我已經知道)請幫助

回答

1

問題是client.call(...)返回Integer而不是String。例如,它可能會返回20(或更確切地說,Integer.valueOf(20)),而您的代碼預計它會返回類似"20"的內容。如果你的目標是轉換20其返回值"20"用於顯示目的,那麼你應該改變這樣的:

text = (String) client.call("mission.addViewer",IpAdress+ ":" + 8214, "newImage"); 

這樣:

text = String.valueOf(client.call("mission.addViewer",IpAdress+ ":" + 8214, "newImage")); 
+0

Waouhhhhhhhhhhhhh!這解決了我一個月的問題。 thx夥計..願上帝保佑你! – youssoua 2012-01-18 18:16:34

+0

@ youssoua:不客氣! – ruakh 2012-01-18 18:53:22