Asmack文件传输错误

时间:2016-04-02 02:01:35

标签: android xmpp openfire asmack

Asmack我正在尝试通过asmack发送文件。

我的连接变量是:

SELECT YEAR(started_at) as 'Year',
MONTH(started_at) as 'Month',
COUNT(user_id) as 'Users'
FROM subs
GROUP BY YEAR(started_at),MONTH(started_at);

这是我的sendFile方法:

   File mf = Environment.getExternalStorageDirectory();
       String filenameWithPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + ThisDemoActivity.this.getApplicationName()+  "/test.txt" ;  
   private XMPPConnection connection;   
   sendFile(filenameWithPath, "username@ipaddress"); 

我能够连接到用户并发送文本,但我无法发送此文件。

输出结果为:

    /* Not very reliable: Sometimes the photos reach to the receiver and sometimes not. It is not at all reliable
     * http://stackoverflow.com/questions/14601198/sending-pictures-like-whatsapp
     */
    public void sendFile(final String path, final String receiver) {

        Thread thread = new Thread() {

            public void run() {

                ServiceDiscoveryManager sdm = ServiceDiscoveryManager
                .getInstanceFor(connection);

                if (sdm == null)
                    sdm = new ServiceDiscoveryManager(connection);

                sdm.addFeature("http://jabber.org/protocol/disco#info");
                sdm.addFeature("jabber:iq:privacy");

                // Create the file transfer manager
                FileTransferManager manager = new FileTransferManager(
                        connection);
                FileTransferNegotiator.setServiceEnabled(connection, true);

                // Create the outgoing file transfer
                OutgoingFileTransfer transfer = manager
                .createOutgoingFileTransfer(receiver +  "/Adium");  //+ "/Smack"

                System.out.println("Receiver of the file is "+receiver+"/Adium");


                Log.i("transfere file", "outgoingfiletransfer is created");

                try {

                    OutgoingFileTransfer.setResponseTimeout(30000);
                    transfer.sendFile(new File(path), "Description");
                    Log.i("transfere file", "sending file");
                    while (!transfer.isDone()) {
                        try {
                            Thread.sleep(1000);
                            Log.i("transfere file", "sending file status "
                                    + transfer.getStatus() + "progress: "
                                    + transfer.getProgress());
                            if (transfer.getStatus() == Status.error) {
                                Log.i("transfere file", "Errorrr isss: "
                                        + transfer.getException());
                                transfer.cancel();
                                break;
                            }
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                } catch (XMPPException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                Log.i("transfere file", "sending file done");
            }
        };
        thread.start();

    }

为什么当我在用户中将ipaddress指定为用户名@ ipaddress / Adium时,没有找到远程服务器?

我也试过这个方法: 方法2:

使用

调用
  04-01 18:58:30.884: I/System.out(16537): Receiver of the file is username@ipaddress/Adium
  04-01 18:58:30.884: I/transfere file(16537): outgoingfiletransfer is created
  04-01 18:58:30.884: I/transfere file(16537): sending file
  04-01 18:58:31.884: I/transfere file(16537): sending file status Negotiating Transferprogress: 0.0
  04-01 18:58:32.844: I/ViewRootImpl(16537): CPU Rendering VSync enable = true
  04-01 18:58:32.884: I/transfere file(16537): sending file status Errorprogress: 0.0
  04-01 18:58:32.884: I/transfere file(16537): Errorrr isss: remote-server-not-found(404)
  04-01 18:58:32.884: I/transfere file(16537): sending file done

我收到以下错误:

  sendFileNew(filenameWithPath);

    private void sendFileNew(String PICKUP_LOC) {
        //configureProviderManager(connection);

        // Create the file transfer manager
        FileTransferNegotiator.setServiceEnabled(connection, true);
        //FileTransferNegotiator.IBB_ONLY = true;
        FileTransferManager manager = new FileTransferManager(connection);

        // Create the outgoing file transfer
        String toName = "username" + "@" + "ipaddress"
                + "/Smack";

        // Create the outgoing file transfer
        final OutgoingFileTransfer transfer = manager
        .createOutgoingFileTransfer(toName);  //+ "/Smack"
        //transfer = manager.createOutgoingFileTransfer(toName);
        Log.i(TAG, "send-TO:  " + toName);

        // Send the file
        try {
            File myFile = new File(PICKUP_LOC);

            if (myFile.exists()) {
                Toast.makeText(XMPPChatDemoActivity.this, "File Exist---", 1).show();
                transfer.sendFile(myFile, "You won't believe this!");
            } else {
                Toast.makeText(XMPPChatDemoActivity.this, "NOt Exist---", 1).show();
            }
        } catch (XMPPException e) {
            e.printStackTrace();
        }
new AsyncTask<Void, Void, Void>() {
            ProgressDialog pd;
            protected void onPreExecute() {
                pd = ProgressDialog.show(XMPPChatDemoActivity.this, "",
                        "Please wait..");
            }
            @Override
            protected Void doInBackground(Void... params) {
                while (!transfer.isDone()) {
                    if (transfer.getStatus().equals("Error")) {
                        System.out.println("ERROR!!! " + transfer.getError());
                    } else if (transfer.getStatus().equals("Cancelled")
                            || transfer.getStatus().equals("Refused")) {
                        System.out.println("Cancelled!!! " + transfer.getError());
                    }
                    try {
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            };

            protected void onPostExecute(Void result) {
                pd.dismiss();
                if (transfer.getStatus().equals("Refused")
                        || transfer.getStatus().equals("Error")
                        || transfer.getStatus().equals("Cancelled")) {
                    Log.i(TAG, "refused cancelled error " + transfer.getException().toString());

                } else {                                    
                    //setMessageToList(new ChatDTO(PICKUP_LOC,
                    //        utilityDAO.getCurrentTime(), "1", "Me")); 
                    Log.i(TAG, "Success: "+ transfer.getFileName());
                }
            };
        }.execute();

    }

这是什么:

     04-01 19:05:13.534: E/AndroidRuntime(18510): FATAL EXCEPTION: main
     04-01 19:05:13.534: E/AndroidRuntime(18510): Process: com.demo.xmppchat, PID: 18510
     04-01 19:05:13.534: E/AndroidRuntime(18510): java.lang.NullPointerException: Attempt to invoke virtual method 'void org.jivesoftware.smackx.ServiceDiscoveryManager.addFeature(java.lang.String)' on a null object reference
     04-01 19:05:13.534: E/AndroidRuntime(18510):   at org.jivesoftware.smackx.filetransfer.FileTransferNegotiator.setServiceEnabled(FileTransferNegotiator.java:126)
     04-01 19:05:13.534: E/AndroidRuntime(18510):   at com.demo.chat.DemoActivity.sendFileNew(XMPPChatDemoActivity.java:771)
     04-01 19:05:13.534: E/AndroidRuntime(18510):   at com.demo.chat.DemoActivity.onOptionsItemSelected(XMPPChatDemoActivity.java:186)
    04-01 19:05:13.534: E/AndroidRuntime(18510):    at android.app.Activity.onMenuItemSelected(Activity.java:2951)
    04-01 19:05:13.534: E/AndroidRuntime(18510):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1180)
    04-01 19:05:13.534: E/AndroidRuntime(18510):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)
    04-01 19:05:13.534: E/AndroidRuntime(18510):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
    04-01 19:05:13.534: E/AndroidRuntime(18510):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)

0 个答案:

没有答案