फेंकता है मैं एक छवि अपलोड करने और इसकी प्रतिक्रिया प्राप्त करने के लिए HttpURLConnection
का उपयोग कर रहा हूं।
यह एम्यूलेटर और मेरे जिओमी डिवाइस पर काम करता है।
हालांकि, यह हमेशा मेरे सोनी डिवाइस पर पर SocketTimeoutException
प्राप्त करता है।
मैंने टाइमआउट को बड़े मूल्य पर 1 मिनट की तरह सेट करने का प्रयास किया है लेकिन काम नहीं किया है।
HttpURLConnection.getInputStream() सॉकेटटाइमआउट एक्सेप्शन
public String uploadFile(File file, String requestURL) {
if (file != null) {
long fileSize = file.length();
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection connection = null;
try {
//config of connection
connection = (HttpURLConnection) new URL(requestURL).openConnection();
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "image/jpeg");
connection.setDoOutput(true);
connection.setRequestProperty("Content-length", "" + fileSize);
connection.connect();
//upload file
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
int bytesRead;
byte buf[] = new byte[1024];
BufferedInputStream bufInput = new BufferedInputStream(new FileInputStream(file));
while ((bytesRead = bufInput.read(buf)) != -1) {
out.write(buf, 0, bytesRead);
out.flush();
}
out.flush();
out.close();
//get response message, but SocketTimeoutException occurs here
BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
StringBuilder sb = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
sb.append(output);
}
//return response message
return output;
} catch (Exception e) {
// Exception
e.printStackTrace();
} finally {
if (connection != null) connection.disconnect();
}
}
return null;
}
इस समस्या का कारण क्या होता है? और इसे कैसे ठीक किया जाए?
अतिरिक्त जानकारी: मैंने उसी वाईफाई कनेक्शन के तहत उपकरणों पर परीक्षण किया। और सुनिश्चित नेटवर्क और फ़ाइल सर्वर ठीक से काम किया। परीक्षण छवियों का फ़ाइल आकार लगभग 100 ~ 200 किलोबाइट है।
क्या सॉकेटटाइमआउट अपवाद तुरंत कॉन्फ़िगर किए गए टाइमआउट के बाद होता है? – Robert
@Robert अपवाद टाइमआउट के बाद होता है – Season
लूप के अंदर फ्लश न करें। – EJP