簡體   English   中英

Android Apps中的網絡和線程

[英]Networking and Threads in Android Apps

我知道我必須使用線程才能在Android應用程序中使用Internet,但是我不知道如何編寫它。 我有一個類稱為“ JabberSmackAPI”-在該類上,我具有通過XMPP登錄,發送和接收功能。

我的應用程序上有一個按鈕,當我按下按鈕時,它應該登錄googleTalk帳戶。

這在Java項目(我可以登錄和發送消息)上很好用,但在Android應用程序項目上卻不行。 我收到此錯誤:“ android.os.NetworkOnMainThreadException”。

我的課是:

public class JabberSmackAPI 
{
    XMPPConnection connection;

    public void login(String userName, String password) throws XMPPException
    {
   ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com",5222,"gmail.com");



        connection = new XMPPConnection(config);

    connection.connect();
    SASLAuthentication.supportSASLMechanism("PLAIN", 0);
    connection.login("email", "password");



    }

    public void sendMessage(String message, String to) throws XMPPException
    {
        Message msg = new Message(to, Message.Type.chat); 
        msg.setBody(message); 
        connection.sendPacket(msg);
        listeningForMessages();

    }




    public void disconnect()
    {
    connection.disconnect();
    }


    public void listeningForMessages() {
        PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class));
        PacketCollector collector = connection.createPacketCollector(filter);
        while (true) {
            Packet packet = collector.nextResult();
            if (packet instanceof Message) {
                Message message = (Message) packet;
                if (message != null && message.getBody() != null)
                    System.out.println("Received message from "
                            + packet.getFrom() + " : "
                            + (message != null ? message.getBody() : "NULL"));
            }
        }
    }

我的應用代碼是:

public class MainActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    setContentView(R.layout.activity_main);



    Button btn1=(Button)findViewById(R.id.button1);
    btn1.setOnClickListener(this);




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onClick(View v) {
    if(v.getId()==R.id.button1)
    {
        try{
            Toast.makeText(this, "T", Toast.LENGTH_LONG).show();



             JabberSmackAPI c = new JabberSmackAPI();
             c.login("username", "password");



        }
        catch(Exception e)
        {
            Log.e("Error","Error in code:"+e.toString());
            e.printStackTrace();
        }
}


    }
}

主應用程序線程應僅用於與接口相關的工作。 您需要使用multithreading ,因為Android應用程序的主線程根本不允許聯網。 由於您的應用程序需要持久的數據連接,因此AsyncTasks也不起作用,因為它們是單一服務的-觸發,獲取結果並關閉連接。

android.os.NetworkOnMainThreadException

確切地說出它的意思-不要在主/ UI線程上進行網絡操作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM