2015-05-29 15 views
2

我用电报API从这个来源: https://github.com/voleon/telegram-trivia-bot 但我的问题是,如何保持用户由于应用程序停止需要用户承租人手机,并得到激活后签署 。来自SMS消息的代码。 我已经实现了保存ApiState对象的Serializable。但是这种方法没有解决我的问题。 这是我ApiState代码:电报API:如何保持ApiState保存签到状态

package engine; 

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.HashSet; 

import org.telegram.api.TLConfig; 
import org.telegram.api.TLDcOption; 
import org.telegram.api.engine.storage.AbsApiState; 
import org.telegram.mtproto.state.AbsMTProtoState; 
import org.telegram.mtproto.state.ConnectionInfo; 
import org.telegram.mtproto.state.KnownSalt; 

/** 
* Created by ex3ndr on 13.01.14. 
*/ 
public class MemoryApiState implements AbsApiState,java.io.Serializable { 

    public HashMap<Integer, ConnectionInfo[]> connections = new HashMap<Integer, ConnectionInfo[]>(); 
    private HashMap<Integer, byte[]> keys = new HashMap<Integer, byte[]>(); 
    private HashMap<Integer, Boolean> isAuth = new HashMap<Integer, Boolean>(); 

    private int primaryDc = 1; 

    public MemoryApiState(boolean isTest) { 
     connections.put(1, new ConnectionInfo[]{ 
       new ConnectionInfo(1, 0, isTest ? "149.154.167.40" : "149.154.167.50", 443) 
     }); 
    } 

    @Override 
    public synchronized int getPrimaryDc() { 
     return primaryDc; 
    } 

    @Override 
    public synchronized void setPrimaryDc(int dc) { 
     primaryDc = dc; 
    } 

    @Override 
    public synchronized boolean isAuthenticated(int dcId) { 
     if (isAuth.containsKey(dcId)) { 
      return isAuth.get(dcId); 
     } 
     return false; 
    } 

    @Override 
    public synchronized void setAuthenticated(int dcId, boolean auth) { 
     isAuth.put(dcId, auth); 
    } 

    @Override 
    public synchronized void updateSettings(TLConfig config) { 
     connections.clear(); 
     HashMap<Integer, ArrayList<ConnectionInfo>> tConnections = new HashMap<Integer, ArrayList<ConnectionInfo>>(); 
     int id = 0; 
     for (TLDcOption option : config.getDcOptions()) { 
      if (!tConnections.containsKey(option.getId())) { 
       tConnections.put(option.getId(), new ArrayList<ConnectionInfo>()); 
      } 
      tConnections.get(option.getId()).add(new ConnectionInfo(id++, 0, option.getIpAddress(), option.getPort())); 
     } 

     for (Integer dc : tConnections.keySet()) { 
      connections.put(dc, tConnections.get(dc).toArray(new ConnectionInfo[0])); 
     } 
    } 

    @Override 
    public synchronized byte[] getAuthKey(int dcId) { 
     return keys.get(dcId); 
    } 

    @Override 
    public synchronized void putAuthKey(int dcId, byte[] key) { 
     keys.put(dcId, key); 
    } 

    @Override 
    public synchronized ConnectionInfo[] getAvailableConnections(int dcId) { 
     if (!connections.containsKey(dcId)) { 
      return new ConnectionInfo[0]; 
     } 

     return connections.get(dcId); 
    } 

    @Override 
    public synchronized AbsMTProtoState getMtProtoState(final int dcId) { 
     return new AbsMTProtoState() { 
      private KnownSalt[] knownSalts = new KnownSalt[0]; 

      @Override 
      public byte[] getAuthKey() { 
       return MemoryApiState.this.getAuthKey(dcId); 
      } 

      @Override 
      public ConnectionInfo[] getAvailableConnections() { 
       return MemoryApiState.this.getAvailableConnections(dcId); 
      } 

      @Override 
      public KnownSalt[] readKnownSalts() { 
       return knownSalts; 
      } 

      @Override 
      protected void writeKnownSalts(KnownSalt[] salts) { 
       knownSalts = salts; 
      } 
     }; 
    } 

    @Override 
    public synchronized void resetAuth() { 
     isAuth.clear(); 
    } 

    @Override 
    public synchronized void reset() { 
     isAuth.clear(); 
     keys.clear(); 
    } 

    public void saveObject() 
    { 
     try 
     { 
      FileOutputStream fileOut = 
      new FileOutputStream("apistate3.tmp"); 
      ObjectOutputStream out = new ObjectOutputStream(fileOut); 
      out.writeObject(this); 
      out.close(); 
      fileOut.close(); 
      System.out.printf("Serialized data is saved"); 
     }catch(IOException i) 
     { 
      i.printStackTrace(); 
     } 
    } 

    public MemoryApiState readObject() 
    { 
     try 
     { 
      FileInputStream fileIn = new FileInputStream("apistate3.tmp"); 
      ObjectInputStream in = new ObjectInputStream(fileIn); 
      MemoryApiState obj = (MemoryApiState) in.readObject(); 
      in.close(); 
      fileIn.close(); 
      return obj; 
     }catch(IOException i) 
     { 
      i.printStackTrace(); 
      return null; 
     }catch(ClassNotFoundException c) 
     { 
      System.out.println("Employee class not found"); 
      c.printStackTrace(); 
      return null; 
     } 
    } 

} 
+0

是否有帮助? تونستیحلشکنی? –

+1

是的,HamidReza答案解决了我的问题。 –

+0

感谢....伙计 –

回答

1

您好:您可以在文件保存状态,任何扩展如下:

private void SaveState(String fileName, MemoryApiState mas) 
{ 

    try  
    { 
    FileOutputStream fileOut = new FileOutputStream(fileName + ".sta"); 
     ObjectOutputStream out = new ObjectOutputStream(fileOut); 
     out.writeObject(mas); 
     out.close(); 
     fileOut.close(); 
    } 
     catch (IOException i) 
     { 
      i.printStackTrace(); 
     } 
} 

,比从文件如下负荷保存的状态:

private MemoryApiState LoadState(String fileName) 
{ 
    try 
     { 


      FileInputStream fileIn = new FileInputStream(fileName + ".sta"); 
      ObjectInputStream in = new ObjectInputStream(fileIn); 
      MemoryApiState mas = (MemoryApiState)in.readObject(); 
      in.close(); 
      fileIn.close(); 

     return mas; 
     } 
    catch (IOException i) 
     { 
      return null; 
     } 
    catch (ClassNotFoundException c) 
     {   
      c.printStackTrace(); 
     } 
      return null; 
} 
+0

我通过这种方法得到“java.io.NotSerializableException:org.telegram.mtproto.state.ConnectionInfo”我该如何解决这个问题? –

+1

你应该添加(实现Serializable)表达式到org.telegram.mtproto.state.ConnectionInfo类和派生类 – HamidReza