K
K
kegd2016-04-04 10:31:47
Java
kegd, 2016-04-04 10:31:47

HttpURLConnection does not work on Android versions older than Lollipop. Where is the mistake?

Good afternoon!
I'm trying to send an Http request using HttpUrlConnection:

DataOutputStream wr;

URL obj = new URL(url);
con = (HttpsURLConnection) obj.openConnection();
con.setConnectTimeout(5000);
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Content-Type", "audio/x-pcm;bit=" + bit + ";rate=" +ar.getSampleRate());
con.setRequestProperty("Transfer-Encoding", "chunked");
con.setDoOutput(true);

wr = new DataOutputStream(con.getOutputStream());
ArrayList<byte[]> Data = new ArrayList<>();
while (isRecording) {
    ar.read(sData, 0, bufferSize / 2);
    byte bData[] = short2byte(sData);
    Data.add(bData);
    os.write(bData, 0, bufferSize);
    wr.write(bData, 0, bufferSize);
}
os.flush();
os.close();

wr.flush();
wr.close();

int responseCode = con.getResponseCode();

The problem is that on Android < 5 version everything works fine. On 5 and 6 the server always returns a 400 response. What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Perelygin, 2016-04-08
@orcDamnar

I think it's a problem with USER-AGENT. Try to use the full line of the existing one and add your own at the end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question