簡體   English   中英

android mms通過mms url下載mms內容

[英]android mms download mms content through mms url

我試圖通過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;
    }

現在我能夠找到解決方案,但我發現有時下載代碼不起作用,但是當你再次嘗試時,雖然我首先建立了與服務器的連接,但它仍然有效。 我在下面提到了連接方法,在此之后調用方法名稱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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM