最近想做一个发送彩信的功能,不是调用系统自带的发送界面。
网上下载了一个 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];}
解决方案 »
- Android中的声音获取进行频率分析
- 韩语和日语交换android编程指导,QQ:1445536693
- android的数据库帮助类,空指针错误。
- 如何使得TextView控件在点击时的焦点总保持在文本的最右端?
- 在android模拟器中如何加载jar包?
- ActivityGroup中调用子Activity的finish方法退出程序的问题
- 如果启动解压下来的apk程序
- 手机插入充电器进行充电,黑屏后,充电指示灯一同熄灭。这个灯应该常亮
- 翻页特效
- android 更改图片的大小,比方说原图片是1800*2400px,现在我需要给定宽度为600px,等比例缩小相当于重新生成一个图片为600*800px
- 选择run->android application后 android device chooser窗口弹出问题
- 问个关于LocationManager定位的问题
[email protected],谢谢!