最近想做一个发送彩信的功能,不是调用系统自带的发送界面。
网上下载了一个 android mms的发送短信的源代码,来发送的。
但是 发送一直失败,提示"Internal Server Error".求高人解答。下面贴出代码(参考之网络上的资料):       Byte[] mbytesToSend =null;
       //这个函数是把彩信内容 转换成 字节数组
        void MMmakePdu()
         {                
                 String subject ="彩信测试";
                 SendReq sendRequest = new SendReq();
                    EncodedStringValue[] sub = EncodedStringValue.extract(subject);
                    if (sub != null & sub.length != 0) {
                            sendRequest.setSubject(sub[0]);
                 }
                    
                    EncodedStringValue[] phoneNumbers = EncodedStringValue.extract("13788888888");
                    if (phoneNumbers != null & phoneNumbers.length != 0){
                            sendRequest.addTo(phoneNumbers[0]);
                    }
                    
                    PduBody pduBody = new PduBody();         
                    
                    PduPart partPdu = new PduPart();
                    partPdu.setCharset(CharacterSets.UTF_8);
                    partPdu.setName("测试".getBytes());
                    partPdu.setContentType("image/png".getBytes());
                    partPdu.setDataUri(new Uri("file://mnt/sdcard//1.png"));
                    pduBody.addPart(partPdu);
                    
                    sendRequest.setBody(pduBody);
                    
                    PduComposer composer = new PduComposer(this, sendRequest);
                    mbytesToSend = composer.make();                    
                   //据说发送彩信需要 APN 切换到 CMWAP下
                    final List<String> list = getSimMNC(this);                     Thread t = new Thread(new Runnable() {                                
                                public void run() {
                                        try{
                                                sendMMMS(TestActivity.this, mbytesToSend);                                                
                                        }catch (Exception e){
                                                e.printStackTrace();
                                        }
                                }
                        });
                    t.start();                    
            }        public static String mmscUrl = "http://mmsc.monternet.com"; //移动
        public static String mmsProxy = "10.0.0.172";
        public static String mmsPort = "80";        
        private static String HDR_VALUE_ACCEPT_LANGUAGE = "";        
        private static final String HDR__KEY_ACCEPT = "Accept";
        private static final String HDR_KEY_ACCEPT_LANGUAGE = "Accept-Language";        
        private static final String HDR_VALUE_ACCEPT = "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic";
               //发送到彩信中心
        public static byte[] sendMMMS(Context context, byte[] pdu) throws Exception
       {
                HDR_VALUE_ACCEPT_LANGUAGE=HTTP.UTF_8;                
                if (mmscUrl == null)
                {
                        throw new IllegalAccessException("URL must not be null");
                }                                
                
                HttpClient client = null;
                try{                                        
                         URI hostUrl = new URI(mmscUrl);
                         HttpHost target = new HttpHost( hostUrl.getHost(), hostUrl.getPort(),  HttpHost.DEFAULT_SCHEME_NAME);                   
                        client = AndroidHttpClient.newInstance("Android-Mms/2.0");
                        HttpPost post = new HttpPost(mmscUrl);
                        ByteArrayEntity entity = new ByteArrayEntity(pdu);
                        entity.setContentType("application/vnd.wap.mms-message");
                        post.setEntity(entity);
                        post.addHeader(HDR__KEY_ACCEPT,HDR_VALUE_ACCEPT);
                        post.addHeader(HDR_KEY_ACCEPT_LANGUAGE, HDR_VALUE_ACCEPT_LANGUAGE);
                        
                        HttpParams params = client.getParams();
                        HttpProtocolParams.setContentCharset(params, "UTF-8");
                        
                        ConnRouteParams.setDefaultProxy(params, new HttpHost(mmsProxy, 80));
        
                        HttpResponse response = client.execute(target,post);//运行到这个函数,就出错了,(Internal Server Error)
                        
                        StatusLine status = response.getStatusLine();
                        System.out.println("status : " + status.getStatusCode());
                        if (status.getStatusCode() != 200)
                        {
                                System.out.println("!200");
                                throw new IOException("HTTP error: " + status.getReasonPhrase());
                        }
                        
                        HttpEntity resentity = response.getEntity();
                        byte[] body = null;
                        
                        if (resentity != null)
                        {
                                try{
                                        if (resentity.getContentLength() <= 0)
                                        {
                                                body  =  new byte[(int)resentity.getContentLength()];
                                                DataInputStream dis = new DataInputStream(resentity.getContent());
                                                
                                                try{
                                                        dis.readFully(body);
                                                }finally{
                                                        try{
                                                                dis.close();
                                                        }catch (IOException e)
                                                        {
                                                                e.printStackTrace();
                                                        }
                                                }
                                        }
                                        
                                }finally
                                {
                                        if (entity != null)
                                                entity.consumeContent();
                                }
                        }
                        System.out.println("result : "+ new String(body));
                        return body;
                        
                }catch (Exception e)
                {
                        e.printStackTrace();
                }
                return new byte[0];}