android mms通过mms url下载mms内容

时间:2013-04-17 12:34:01

标签: android mms msisdn

我正在尝试通过MMS网址下载MMS图片内容,但它返回403(禁止)服务器响应,其中包含无效的MSISDN个数字。我已粘贴下面的代码供参考。提前谢谢!

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;
    }

1 个答案:

答案 0 :(得分:3)

现在我能够找到解决方案,但我发现有时候下载代码不起作用,但是当你再次尝试时它会工作,虽然我缺少首先建立与服务器的连接 。我在下面提到了连接方法,在此之后调用方法名称downloadThroughGateway(参数),这个问题代码就提到了。

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);
    }