service unavailable response from server while sen

2019-07-04 17:33发布

问题:

I am trying to send custom information with IQ in asmack from android.

So I am using below code to send the custom IQ message.

public void onClick(View arg0) {

            CustomIQ req = new CustomIQ();
            req.myData="Hello world";
            req.setType(IQ.Type.GET);
            req.setTo(Activity_title+Constants.DOMAIN);
            MainActivity.connection.sendPacket(req);
            Log.d(Constants.TAG, "xml value :"+req.toXML());
        Log.d(Constants.TAG, "child element value :"+req.getChildElementXML());
            Log.d(Constants.TAG, " custom IQ req sent");

Below is my custom IQ class implementation:

import org.jivesoftware.smack.packet.IQ;

public class CustomIQ extends IQ {

    String myData;

    @Override
    public String getChildElementXML() {

        String request = "<query xmlns='myxmlns'>"
                        + "<myData>"+ myData + "</myData>"
                        + "</query>";

        return request;
    }

}


            }

But after the sending the custom IQ, I am getting in the IQ listener as Service Unavailable and error code as 503.

Below are the request to server :

xml value :<iq id="BTn30-5" to="swathi@btp121374" type="get"><query xmlns='myxmlns'><myData>Hello world</myData></query></iq>
child element value :<query xmlns='myxmlns'><myData>Hello world</myData></query>

Below is the response from server:

xml value :<iq id="BTn30-5" to="ganesh@btp121374/Smack" from="swathi@btp121374" type="error"><query xmlns='myxmlns'><myData>Hello world</myData></query><error code="503" type="CANCEL"><service-unavailable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>

So what is the reason for I am getting response from server as Service Unavailable.

Edit:

I implemented the IQProvider as below

public class customIQProvider implements IQProvider{

    @Override
    public IQ parseIQ(XmlPullParser parser) throws Exception {

        Log.d(Constants.TAG, "In custom IQ provider");
        CustomIQ myIQ_ref = new CustomIQ();


        int eventType = parser.next();

        while(eventType == XmlPullParser.START_TAG){

            switch(parser.getEventType()){
            case XmlPullParser.START_TAG:
            {
                if(parser.getName().equals("myData")){
                     myIQ_ref.myData=(parser.nextText());
                }
            }
            return myIQ_ref;
            }
        }

    return null;
    }

}

回答1:

I believe you're falling foul of the iq routing rules in XMPP. If you send an iq stanza to "swathi@btp121374", you're not asking it to be routed to a client, you're asking the btp121374 server to handle it on behalf of swathi@btp121374.

Given the resource of the sending JID, I imagine you want to send to "swathi@btp121374/Smack" or similar. Sending it to the full JID (JID including resource) tells the server to route it to the client instead of handling it itself.

(Note that the routing rules for presence, message and iq are different - the above only applies to iq)



回答2:

Obviously the server doesn't know how to handle your new namespace, unless you also implement it on the server. What do you expect the server to do with your custom element?

Also, it's generally a bad idea to generate XML by concatenating strings. If myData contains and <, or any of the other characters that need to be escaped, your XML will be invalid. Smack surely has better ways to generate your custom data.