android mms download mms content through mms url
Asked Answered
T

1

13

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;
    }
Tourist answered 17/4, 2013 at 12:34 Comment(16)
What about user access credentials? Did you check whether the right credentials are set for data service access. Did you try using (Network Protocol Analyzer) tools such as WireShark etc.. to decipher the MMS submit request (HTTP PDU) and the corresponding response from the remote MMSC. Without providing any details regarding the connection such as the remote MMSC URL, log in credentials its difficult to reproduce and provide a fix for the issue.Phelia
how to pass the credentials and I also didn't have any information about the credential that need to be provide?Tourist
First please check if you're able to access and launch google.com on your browser successfully. Then you can navigate to the MMS specific settings by navigating to (On android 2.0) Settings-> Mobile networks -> Access Point Names -> Your Network Providor's Name . Under here you will find Tabs such as 1.)MMSC 2.) MMS proxy 3.) MMS port 4.) Username 5.) Password.Phelia
Once you got the right credentials set on the phone under the above mentioned tabs, please refer to the post (android.riteshsahu.com/tips/…) to set the connection parameters by programatically reading these credentials.Phelia
thanks for your response... it may be the exact way as you have describe but on the airtel and idea operator you can see there is no any password and username assign.. it's blank.. if you have done the mms downloading then let me show the code snippets..Tourist
Actually you can observe additional info here #14453308Fiorenze
Hi Artemis 1'st thanks for your response by the way I didn't find any way to download the mms content although I have extracted all the required mms information.Tourist
Kamal, Maybe its too late. Anyways, basically you'll have to construct a WSP/HTTP GET request to the URL which you extracted from the MMS notification message. This is the only way by which you'll be able to download the MMS message to your phone. This post should be useful for that : (#1486208).Phelia
@Sriram the issue is not what the solution you have mention, as you can see we do it by GET, the issue is how to authenticate the particular number with the operator server..Tourist
@kamal_tech_view: I did inquire about the user access credentials from the very first comment that I posted here, didn't I? The reason I hinted at WSP/HTTP GET in my second comment was because of you saying "I didn't find any way to download the mms content although I have extracted all the required mms information". What was that all about? Did you check with the service provider (Idea/Airtel) so as to why the (MMS specific settings) required fields are blank?Phelia
@Sriram: My application need those required field and the default messaging app in phone didn't ... does that make any sense ?Tourist
Anything @Tourist ? I get from time to time a timed out exception...Richellericher
@Richellericher : I guess you have ensureRouteToHost() method, you should ensure that it works perfectly and you can post your code as a answer.Tourist
@Tourist I do have ensure route to host but it keeps on throwing an IOException when trying to connect to the proxyRichellericher
can you post your code snippets so, that I can go in detail through it..Tourist
hi all I have found the solution for this issue please find the answer as mention.Tourist
T
3

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);
    }
Tourist answered 28/5, 2013 at 5:0 Comment(1)
I am doing something similar HERE!!! #21748709Glabella

© 2022 - 2024 — McMap. All rights reserved.