我在android开源中复制所有代码形式的mms pacakge,然后我使用这些代码发送mms:
public class Mms_sampleActivity extends Activity {
String subject = "test MMS ";
String recipient = "13534585169"; //13800138000
SendReq sendRequest;
EncodedStringValue[] sub;
byte[] bytesToSend;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MMmakePdu();
}
void MMmakePdu()
{
sendRequest = new SendReq();
sub = EncodedStringValue.extract(subject);
if (sub != null & sub.length != 0) {
sendRequest.setSubject(sub[0]);
}
EncodedStringValue[] phoneNumbers = EncodedStringValue.extract(recipient);
if (phoneNumbers != null & phoneNumbers.length != 0)
{
sendRequest.addTo(phoneNumbers[0]);
}
PduBody pduBody = new PduBody();
PduPart part = new PduPart();
part.setName("sample".getBytes());
part.setContentType("image/png".getBytes());
String furl = "file://mnt/sdcard//1.png";
PduPart partPdu = new PduPart();
partPdu.setCharset(CharacterSets.UTF_8);
partPdu.setName(part.getName());
partPdu.setContentType(part.getContentType());
partPdu.setDataUri(Uri.parse(furl));
pduBody.addPart(partPdu);
sendRequest.setBody(pduBody);
PduComposer composer = new PduComposer(this, sendRequest);
bytesToSend = composer.make();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try{
SendMMS.sendMMMS(Mms_sampleActivity.this, bytesToSend);
}catch (Exception e){
e.printStackTrace();
}
}
});
t.start();
}
}
public class SendMMS {
public static String mmscUrl = "http://mmsc.monternet.com";
//public static String mmscUrl = "http://mmsc.myuni.com.cn";
//public static String mmscUrl = "http://www.baidu.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_AVLUE_ACCEPT_LANGUAGE = getHttpAcceptLanguage();
if (mmscUrl == null)
{
throw new IllegalAccessException("URL must not be null");
}
HttpClient client = null;
try{
//client = AndroidHttpClient.newInstance("Android-Mms/2.0");
//client = HttpConnector.buileClient(context);
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));
//req.setParams(params);
HttpResponse response = client.execute(target,post);
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];
}
}
我的代码运行没有任何错误,但是给我status.getStatusCode()=400
,我不知道我的partPdu
是错误还是httppost
是错误的,你能给我一些建议吗?