0

情况:函数调用线程向服务器发送数据。这个线程反过来又产生另一个线程,使用ObjectInputStream()从服务器获得结果。Android:FutureTask无法投射到MyClass

最后,这个对象被派生线程返回给调用函数。

注意:线程是可调用并且是同步

问题:我得到一个例外, “FutureTask不能转换到MyClassName”。

我在网上找不到解决方案。有什么建议么 ?

客户端代码

public synchronized Object invoke(Object proxy, Method m, Object[] args) throws Throwable 
       { 

        try { 

// Lots of if-else statements 
//......... 

        else if (m.toString().contains("getPosition")) 
        {      
         if (!offload){ 
         Log.d("DProxy","Sprite Redirection takes place here 6 "+m.invoke(v, args).toString()); 

         //System.out.println("PROXY Tick Argument is "); 

         return m.invoke(v, args); 
         } 
         else 
         { 
           //Code to create THREAD on the Endpoint 

         if (endpoint !=null) 
         { 

          if (!serialized) 
          {  
            System.out.println("serializing via proxy itself 11"); 
            this.endpoint.serialize(obj); 
            serialized = true; 
          } 

          Object[] args1 = new Object[args.length+1]; 

          for (i=0;i<args.length;i++) 
           args1[i]=args[i]; 

          args1[i]= m.toString(); 

// ** Error is thrown at this line below** 
    Vec2 tmp = (Vec2) this.endpoint.onClick(args1); 

           return tmp; 
           //return null; 
          } 
          else 
           System.out.println("Endpoint is NULL"); 
         } 
        } 

的onclick()方法。

public synchronized Object onClick(Object[] args) { 
    try { 
      latch.await(); 
      ctr++; 
       Log.d("CLIENT","Sending Param to Client "+args[args.length-1].toString()+" "+ctr); 

     objectOutputStream.writeBoolean(false); 

     // TEMP Commented 
     objectOutputStream.flush(); 
     objectOutputStream.reset(); 
     objectOutputStream.writeObject(args); 

     Callable<Object> worker = (Callable<Object>) new ClientThread(thisSocket,ctr); 
     return executor.submit(worker); 

}catch (Exception e) 
{ 
    Log.e("ENDPOINT Exception",e.toString()); 
} 
     Log.e("ENDPOINT","Returning blank Object"); 
     return new Object(); 
    }    

class ClientThread implements Callable <Object>{//Runnable { 

    private int ctr; 

    public ClientThread(Socket socket, int ctr) 
    { 
     this.ctr = ctr; 
    } 

    public synchronized Object call() { 
     Vec2 res1 = null; 
     Double res2=null; 
     Object res = null; 

     try { 

      Log.v("CLIENT","Attempting to receive results from Server "+ctr); 
      res = objectInputStream.readObject(); 

      if (res instanceof Vec2) 
       { 
        res1 = (Vec2) res; 
        Log.v("CLIENT", "Object received Vec2 "+ctr); 
       } 
      else if (res instanceof Double) 
       { 
        res2 = (Double) res; 
        Log.v("CLIENT", "Object received Double "+ctr); 
       } 
      else 
       if(res==null) 
        Log.v("CLIENT", "Object received is NULL "+ctr); 
       else 
        Log.v("CLIENT", "Object received of UNKNOWN Type "+ctr); 


    } catch (UnknownHostException e1) { 
      Log.e("CLIENT receive error 1",e1.toString()); 
     } catch (IOException e1) { 
      Log.e("CLIENT receive error 2",e1.toString()); 

     } 
     catch (Exception e) 
     { 
      Log.e("CLIENT receive error 3",e.toString()); 
     } 
     if (res1 !=null) 
     return res1; 
     else 
      if (res2!=null) 
       return res2; 
      else 
       return res; 
    } 
    //} 

} 
+1

是MyClassName的延伸FutureTask? – donfuxx

+0

如果你会发布你的代码样本,它会更容易回答你的问题 – MikeL

+0

不,不。迈克尔,我已经在原帖中添加了源代码。 MyClassName实际上是Vec2。 – Abhishek

回答

1

任何对象都不能被转换为任何类或接口,除了扩展/实现的类/接口。 FutureTask实现接口RunnableFutureRunnableFuture

0

现在行了。我只是将ClientThread的代码实现为一个函数调用!

奇怪的是Java的方式.....

相关问题