I found a way that appears to work on every device I have:
URL url = new URL(apn);
// Set-up proxy
Log.d(TAG, "Setting up proxy (" + host + ":" + port + ")");
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost", host);
systemProperties.setProperty("http.proxyPort", String.valueOf(port));
systemProperties.setProperty("http.keepAlive", "false");
// Open connection
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Use the URL connection for output
connection.setUseCaches(false);
connection.setDoOutput(true);
// Set the timeouts
connection.setConnectTimeout(TIMEOUT);
connection.setReadTimeout(TIMEOUT);
// Sends the request
connection.setRequestProperty("Content-Type", "application/vnd.wap.mms-message");
connection.setRequestProperty("Content-Length", Integer.toString(mms.length));
// Connect to the MMSC
Log.d(TAG, "Connecting to APN " + apn);
connection.connect();
try
{
// Upload mms as bytes array
Log.d(TAG, "Uploading data (Size: " + mms.length);
OutputStream out = connection.getOutputStream();
out.write(mms);
out.flush();
out.close();
// Wait for the response
Log.d(TAG, "Response code is " + connection.getResponseCode());
if (connection.getContentLength() >= 0)
{
Log.d(TAG, "Reading response ...");
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();
// Close the connection
Log.d(TAG, "[MMS Sender] Disconnecting ...");
connection.disconnect();
}
else
{
return false;
}
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
I hope this would help some people with the same erro.