android mms通过mms url下载mms内容

2024-04-07

我正在尝试下载MMS通过 MMS url 获取图片内容,但返回 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;
    }

现在我能够找到解决方案,但我发现有时下载代码不起作用,但当您重试时它会起作用,尽管我缺少什么首先建立与服务器的连接。我在下面提到了连接方法,并在此调用之后调用了该问题代码中提到的方法名称 downloadThroughGateway(parameters)。

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);
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android mms通过mms url下载mms内容 的相关文章

随机推荐

  • Little Endian 并推入 nasm

    我不明白为什么代码的输出是Ole 小字节序不应该影响push命令 global start section data x dd 3 section text start mov eax 4 mov ebx 1 mov dword x 0x0
  • C++: std::tie 与 std::ignore 的返回类型

    我想知道 C 11 标准是否给出了关于type of the std tuple由返回std tie当一些论点是std ignore 更具体地说 我可以假设 decltype std tie 42 std ignore 不等于decltyp
  • 64 位 Microsoft SQL Server 数据工具

    我无法找到 64 位版本的 Microsoft SQL Server Data Tools 有谁知道哪里可以下载64位版本Microsoft SQL Server 数据工具 Visual Studio 2013 和 SQL Server 2
  • 在 Swift 中检查 iOS 设备是否有 LiDAR

    Swift 有没有办法检查设备是否有 LiDAR 传感器 不幸的是 我在苹果官方纪录片和互联网搜索中都没有找到任何信息 我当前的解决方法是确定设备类型 如本文中所述 如何确定当前的iPhone 设备型号 https stackoverflo
  • Sencha Touch 2 - 无法从控制器获取参考视图

    我正在尝试通过参考查看 在控制器中定义的方法内 我收到错误消息 对象 object Object 没有方法 getUserForm 这是我的视图和控制器 如果有人可以帮助我 我会很高兴 提前致谢 用户表格 Ext define appNam
  • .htaccess url重写

    我想重写一个URL 假设您访问http files domain com uploads file name jpg http files domain com uploads file name jpg 我如何使用 htaccess 来编
  • Python - 导入函数和本地声明函数之间的性能差异?

    在 Python 中导入函数与在当前文件中声明函数在性能上是否存在显着差异 我有一个小函数 单行 我经常在几个函数中使用它 py我的程序中的文件 我希望只定义一次 以便我对它所做的更改反映在各处 但是 我不确定将它用作导入函数是否会在调用它
  • 使 (Mac)Vim 重新打开并在关闭时打开文件

    使用案例 MacVim 窗口中显示 2 3 个文件 您按 ctrl Q MacVim 就会关闭 当您重新启动 MacVim 时 它会打开并显示与您关闭它时相同的文件 你怎么能这么做呢 ANSWER 我将以下内容添加到我的 vmirc sav
  • 组合 Spark UDF(而不是将 UDF 作为一个)时是否会造成性能损失?

    我想知道编写 Spark udf 是否会降低性能 一般来说 我更喜欢编写只做一件事的小函数 这是一个简单的例子 给定一个DataFrame df def inc udf i Double gt i 1 def double udf i Do
  • Gson:java.text.ParseException:无法解析的日期:“2018-04-09T09:00:00 + 02:00”

    如何解析以下格式的字符串日期 2018 04 09T09 00 00 02 00 Gson使用 new SimpleDateFormat yyyy MM dd T HH mm ss Z Locale US 但它给出了以下例外 com goo
  • Python:为什么要腌制?

    我一直在使用pickle并且很高兴 然后我看到了这篇文章 不要腌制你的数据 http www benfrederickson com 2014 02 12 dont pickle your data html 进一步阅读似乎是 泡菜很慢 h
  • 如何中止存储桶的所有不完整分段上传

    有时 分段上传会因某种原因挂起或无法完成 在这种情况下 您将陷入难以删除的孤立部分 您可以通过以下方式列出它们 aws s3api list multipart uploads bucket BUCKETNAME 我正在寻找方法来中止它们
  • 如何更改过滤器中 http 响应的正文

    我正在尝试使用过滤器来检查响应正文中的 HTML 标记 问题是 如果我改变过滤器中的主体 当它到达客户端时它不会改变 我尝试了此处显示的解决方案 寻找使用 servlet 过滤器将内容插入响应的示例 https stackoverflow
  • Laravel 5:如何将播种器类添加到自动加载?

    我遵循文档 http laravel com docs master migrations database seeding http laravel com docs master migrations database seeding
  • 按 eloquent 中的最佳匹配排序

    我从雄辩的查询中得到了一些结果 我想按最佳匹配对它们进行排序 我怎样才能在 laravel eloquent 中做到这一点 在这里我找到了一些 SQL 解决方案 但我无法在 eloquent 构建器中使用它 SELECT TOP 5 FRO
  • 使用 ogr2ogr 将 svg 转换为 geojson 失败

    我从以下位置下载了芬兰的 svg 地图http www amcharts com svg maps map finland http www amcharts com svg maps map finland 我想将其转换为 topojso
  • 无法在 VSCode 中使用 pygame

    我目前正在做一项任务 我必须完成一些功能并导入 pygame 以便我可以运行一个跑步程序 当我尝试通过运行命令导入 pygame 时pip3 install r requirements txt 有一个需求文本文件 里面只写着 pygame
  • Azure Service Fabric 节点、节点类型、实例和规模集

    在尝试了几天Azure的Service Fabric之后 我仍然对以下四个关键词感到不舒服 实例 节点 节点类型 规模设定 他们的意思是什么 有什么区别 Instance 取决于上下文 它可能意味着虚拟机 服务实例等 Node 集群内的节点
  • Moment.js 如何使用 fromNow() 在几小时内返回所有内容?

    我已经搜索过moment js 文档 http momentjs com docs and 堆栈溢出 https stackoverflow com 的一种使用方法fromNow 功能但在几小时内返回所有内容 我的意思是 moment 20
  • android mms通过mms url下载mms内容

    我正在尝试下载MMS通过 MMS url 获取图片内容 但返回 403 禁止 服务器响应 其中包含无效内容MSISDN数字 我已将我的代码粘贴在下面以供参考 提前致谢 private static boolean downloadThrou