5

फेंकता है मैं एक छवि अपलोड करने और इसकी प्रतिक्रिया प्राप्त करने के लिए 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 किलोबाइट है।

+0

क्या सॉकेटटाइमआउट अपवाद तुरंत कॉन्फ़िगर किए गए टाइमआउट के बाद होता है? – Robert

+0

@Robert अपवाद टाइमआउट के बाद होता है – Season

+0

लूप के अंदर फ्लश न करें। – EJP

उत्तर

0

क्योंकि आपने दस सेकंड के पढ़ने का समय निर्धारित किया है और दस सेकंड के भीतर कोई प्रतिक्रिया प्राप्त नहीं हुई है।

क्या यह एक चाल प्रश्न है?

एनबी आपको सामग्री-लंबाई शीर्षलेख सेट करने की आवश्यकता नहीं है। जावा आपके लिए ऐसा करेगा।

+1

मुझे पता है कि लंबे समय तक पढ़ने के कारण अपवाद होता है। लेकिन, मेरे डिवाइस में से कोई एक काम क्यों नहीं करता है, जबकि अन्य डिवाइस/एमुलेटर काम करता है? वे एक ही वाईफाई कनेक्शन का उपयोग कर रहे हैं। – Season

+0

दस सेकंड कम मूल्य नहीं हैं –

+0

@MicheleBontorno मैं इससे सहमत नहीं हो सकता। न तो आप सर्वर के औसत प्रतिक्रिया समय और इसके भिन्नता को जानने के बिना भी कर सकते हैं। – EJP

0

बस कनेक्शन.सेट रीडटाइमआउट() कथन को हटा दें क्योंकि डिफ़ॉल्ट रूप से यह रीडटाइमआउट मान 0 पर सेट करेगा यानी डेटा उपलब्ध होने तक डेटा की प्रतीक्षा करेगा। इसलिए, आपको सॉकेटटाइम आउट अपवाद नहीं मिल सकता है।

संबंधित मुद्दे