问题描述
我试图下载通过MMS URL的彩信
图片内容,但它有一个403(禁止)服务器的响应无效返回MSISDN
号。我已经贴我的code以下,以供参考。在此先感谢!
私有静态布尔downloadThroughGateway(上下文的背景下,字符串主机,
串口,串urlMms)抛出异常{
网址URL =新的URL(urlMms);
//设置了代理
如果(主机= NULL和放大器;!&安培;端口=空&放大器;!&安培; host.equals()及和放大器;!port.equals()){
Log.d(TAG,[MMS接收器]设置代理服务器(主机+:+端口
+));
属性systemProperties = System.getProperties();
systemProperties.setProperty(http.proxyHost,主持人);
systemProperties.setProperty(把http.proxyPort,口);
systemProperties.setProperty(http.keepAlive,假);
}
//打开连接
HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();
//禁用缓存
connection.setUseCaches(假);
//设置超时
connection.setConnectTimeout(超时);
connection.setReadTimeout(超时);
//连接到MMSC
Log.d(TAG,[MMS接收器]连接到MMS地址+ urlMms);
connection.connect();
尝试 {
Log.d(TAG,
[彩信接收器]响应code是
+ connection.getResponse code());
如果(connection.getContentLength()> = 0){
Log.d(TAG,[MMS接收器]下载MMS数据(尺寸:
+ connection.getContentLength()+));
byte []的responseArray =新的字节[connection.getContentLength()];
我的DataInputStream =新的DataInputStream(
connection.getInputStream());
INT B = 0;
INT索引= 0;
而((二= i.read())!= - 1){
responseArray [指数] =(字节)B:
指数++;
}
我关上();
//解析响应
MmsDe codeR分析器=新MmsDe codeR(responseArray);
parser.parse();
字节[] [] imageBytes =新的字节[parser.getParts()的大小()。] [];
对于(INT J = 0; J< parser.getParts()的大小(); J ++){
imageBytes [J] = parser.getParts()得到(J).getContent()。
}
//插入到数据库
//乌里msgUri = MmsHelper.insert(背景下,parser.getFrom()
// parser.getSubject(),imageBytes);
// ContentValues updateValues =新ContentValues();
// updateValues.put(读,0);
// context.getContentResolver()。更新(msgUri,updateValues,
// 空值,
// 空值);
//关闭连接
Log.d(TAG,[MMS接收器]断开......);
connection.disconnect();
System.gc()的;
// 回电话
//如果(BI!= NULL)
// bi.onReceiveMms(背景下,msgUri);
返回true;
}
//关闭连接
Log.d(TAG,[MMS接收器]断开......);
connection.disconnect();
}赶上(例外五){
e.printStackTrace();
}
返回false;
}
现在我能找出解决办法,但我发现,有时下载code不工作,但是当你再试试它的工作原理虽然什么我缺少的首先建立到服务器的连接。我已经提到下面的连接方法,并在此之后调用的方法名称downloadThroughGateway(参数),这是提到在这个问题上code。
私人无效startConnectivity()抛出异常{
ConnectivityManager mConnMgr =(ConnectivityManager)上下文
.getSystemService(Context.CONNECTIVITY_SERVICE);
如果(!mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS)
.isAvailable()){
抛出新的异常(尚未公布);
}
诠释计数= 0;
INT结果= beginMmsConnectivity(mConnMgr);
如果(结果!= PhoneEx.APN_ALREADY_ACTIVE){
的NetworkInfo信息= mConnMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS);
而(!info.isConnected()){
视频下载(1500);
信息= mConnMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS);
Log.d(>>>中,等待连接:状态=+ info.getState());
如果(计数++盐; 5)
抛出新的异常(无法连接);
}
}
视频下载(1500);
}
I am trying to download the MMS
picture content through the MMS url, but it returns with a 403 (Forbidden) server response with an invalid MSISDN
number. I have pasted my code below for reference. Thanks in advance!
private static boolean downloadThroughGateway(Context context, String host,
String port, String urlMms) throws Exception {
URL url = new URL(urlMms);
// Set-up proxy
if (host != null && port != null && host.equals("") && !port.equals("")) {
Log.d(TAG, "[MMS Receiver] Setting up proxy (" + host + ":" + port
+ ")");
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost", host);
systemProperties.setProperty("http.proxyPort", port);
systemProperties.setProperty("http.keepAlive", "false");
}
// Open connection
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Disable cache
connection.setUseCaches(false);
// Set the timeouts
connection.setConnectTimeout(TIMEOUT);
connection.setReadTimeout(TIMEOUT);
// Connect to the MMSC
Log.d(TAG, "[MMS Receiver] Connecting to MMS Url " + urlMms);
connection.connect();
try {
Log.d(TAG,
"[MMS Receiver] Response code is "
+ connection.getResponseCode());
if (connection.getContentLength() >= 0) {
Log.d(TAG, "[MMS Receiver] Download MMS data (Size: "
+ connection.getContentLength() + ")");
byte[] responseArray = new byte[connection.getContentLength()];
DataInputStream i = new DataInputStream(
connection.getInputStream());
int b = 0;
int index = 0;
while ((b = i.read()) != -1) {
responseArray[index] = (byte) b;
index++;
}
i.close();
// Parse the response
MmsDecoder parser = new MmsDecoder(responseArray);
parser.parse();
byte[][] imageBytes = new byte[parser.getParts().size()][];
for (int j = 0; j < parser.getParts().size(); j++) {
imageBytes[j] = parser.getParts().get(j).getContent();
}
// Insert into db
// Uri msgUri = MmsHelper.insert(context, parser.getFrom(),
// parser.getSubject(), imageBytes);
// ContentValues updateValues = new ContentValues();
// updateValues.put("read", 0);
// context.getContentResolver().update(msgUri, updateValues,
// null,
// null);
// Close the connection
Log.d(TAG, "[MMS Receiver] Disconnecting ...");
connection.disconnect();
System.gc();
// Callback
// if (bi != null)
// bi.onReceiveMms(context, msgUri);
return true;
}
// Close the connection
Log.d(TAG, "[MMS Receiver] Disconnecting ...");
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
Now I am able to find out the solution but I find that sometime the download code doesn't work but when you try again it works although what I was missing first establishing the connectivity to the server. I have mention below the connectivity method and after this call the method name downloadThroughGateway(parameters) which is mention on this question code.
private void startConnectivity() throws Exception {
ConnectivityManager mConnMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (!mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS)
.isAvailable()) {
throw new Exception("Not available yet");
}
int count = 0;
int result = beginMmsConnectivity(mConnMgr);
if (result != PhoneEx.APN_ALREADY_ACTIVE) {
NetworkInfo info = mConnMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS);
while (!info.isConnected()) {
Thread.sleep(1500);
info = mConnMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS);
Log.d(">>>", "Waiting for CONNECTED: state=" + info.getState());
if (count++ > 5)
throw new Exception("Failed to connect");
}
}
Thread.sleep(1500);
}
这篇关于Android的彩信下载彩信通网址彩信内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!