电报API RPC超时异常

时间:2016-10-02 16:53:27

标签: android eclipse telegram

我正在使用电报API来开发我的应用程序。这是整个代码。 app_info.java:

package com.example.mytelegram;
import org.telegram.api.engine.AppInfo;
public class app_info extends AppInfo {

public app_info(int apiId, String deviceModel, String systemVersion,
        String appVersion, String langCode) {
    super(apiId, deviceModel, systemVersion, appVersion, langCode);
    // TODO Auto-generated constructor stub
}

MemoryApiState.java:

package com.example.mytelegram;

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;

import java.util.ArrayList;
import java.util.HashMap;

public class MemoryApiState implements AbsApiState {

private 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 = 2;  // I have tested application with primaryDc=0 to 10

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();


}
}

MainActivity.java:

package com.example.mytelegram;

import java.io.IOException;

import org.telegram.api.TLAbsUpdates;
import org.telegram.api.TLConfig;
import org.telegram.api.TLNearestDc;
import org.telegram.api.auth.TLAuthorization;
import org.telegram.api.auth.TLSentCode;
import org.telegram.api.engine.ApiCallback;
import org.telegram.api.engine.AppInfo;
import org.telegram.api.engine.LoggerInterface;
import org.telegram.api.engine.RpcException;
import org.telegram.api.engine.TelegramApi;
import org.telegram.api.requests.TLRequestAuthSendCall;
import org.telegram.api.requests.TLRequestAuthSendCode;
import org.telegram.api.requests.TLRequestAuthSignIn;
import org.telegram.api.requests.TLRequestHelpGetConfig;
import org.telegram.api.requests.TLRequestHelpGetNearestDc;
import org.telegram.api.requests.TLRequestUpdatesGetState;
import org.telegram.api.updates.TLState;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;


public class MainActivity extends ActionBarActivity {

static Context context;
private static AppInfo ai;



private static void createApi() throws IOException {

    String res = "No test";
    boolean useTest = res.equals("test");

    apiState = new MemoryApiState(useTest);
    ai=new AppInfo(App_ID, "Sony", "???", "???", "en");
    api = new TelegramApi(apiState, ai, new ApiCallback() {

        @Override
        public void onAuthCancelled(TelegramApi api) {

        }

        @Override
        public void onUpdatesInvalidated(TelegramApi api) {

        }

        @Override
        public void onUpdate(TLAbsUpdates updates) {

            Log.d("tlg", "onUpdate(TLAbsUpdates updates)");
        }
    });
}


 private static void login() throws IOException {
     //TLNearestDc dcInfo = api.doRpcCallNonAuth(new TLRequestHelpGetNearestDc());
       // api.switchToDc(dcInfo.getNearestDc());

        //************ Exception raises here ************
        TLConfig config = api.doRpcCallNonAuth(new TLRequestHelpGetConfig());  // I have An IOException Here (e=timeout Exception)
        apiState.updateSettings(config);
        Log.d("tlg","completed.");




        String phone = "+9891********"; // I have tested app with the phone without plus too
        Log.d("tlg","Sending sms to phone " + phone + "...");



        TLSentCode sentCode = null;
        try {
             api.doRpcCallNonAuth(new TLRequestAuthSendCode(phone, 1, app_id, "__hash__", "en"));
        } catch (RpcException e) {
            if (e.getErrorCode() == 303) {
                int destDC;
                if (e.getErrorTag().startsWith("NETWORK_MIGRATE_")) {
                    destDC = Integer.parseInt(e.getErrorTag().substring("NETWORK_MIGRATE_".length()));
                } else if (e.getErrorTag().startsWith("PHONE_MIGRATE_")) {
                    destDC = Integer.parseInt(e.getErrorTag().substring("PHONE_MIGRATE_".length()));
                } else if (e.getErrorTag().startsWith("USER_MIGRATE_")) {
                    destDC = Integer.parseInt(e.getErrorTag().substring("USER_MIGRATE_".length()));
                } else {
                    throw e;
                }
                api.switchToDc(destDC);
                sentCode = api.doRpcCallNonAuth(new TLRequestAuthSendCode(phone, 0, app_id, "__hash__", "en"));
            } else {
                throw e;
            }
        }
        Log.d("tlg","Activation code:");
        String code = "123";
        TLAuthorization auth = api.doRpcCallNonAuth(new TLRequestAuthSignIn(phone, sentCode.getPhoneCodeHash(), code));
        apiState.setAuthenticated(apiState.getPrimaryDc(), true);
        Log.d("tlg","Activation complete.");
        TLState state = api.doRpcCall(new TLRequestUpdatesGetState());
        Log.d("tlg","loaded.");

 }





 static MemoryApiState apiState;
 static TelegramApi api;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=getApplicationContext();

    try {
        createApi();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {

        login();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //workLoop();

}
}

我在我的eclipse&#39; libs&#39;中添加了telegram-api库(telegram-api-1.1.127-shadow.jar)。夹。当我想调用RPC时,MainActivity.java中的问题是Timeout Exception。问题出在哪儿?我该怎么办?我的操作系统是Windows 8.1。我整整都在寻找。我也读过github代码。它们对我来说非常复杂。我不知道该怎么办? 我已将INTERNET和ACCESS_NETWORK_STATE权限添加到AndroidManifest.xml。我总是在应用程序运行时检查网络连接。

1 个答案:

答案 0 :(得分:-1)

我在您的代码中发现了问题:

MemoryApiState 中,您应该放入&#39; "Names:Cummins, Inc. Cummins Engine Company, Inc.| Variant title:Dodge Ram 24 valve turbo diesel, 2000.0 model year wiring diagram :bull. no. 3666481.|" &#39;地图,&#39; "Names:| Variant title: |" &#39;作为关键而不是&#39; 1&#39;。