2009-07-13 8 views
13

पर असफल (हल - नीचे टिप्पणी देखें)HttpURLConnection एंड्रॉयड

मैं एक वर्ग है कि एक बहुखण्डीय फाइल अपलोड लागू करता है। कोड प्रत्येक जावा क्लाइंट पर काम करता है जिसे मैंने एंड्रॉइड को छोड़कर इसे आजमाया है, और यह मेरे एंड्रॉइड ऐप में एकमात्र HTTP अनुरोध कोड है जो मेरी बैक एंड सर्विस के साथ अच्छा नहीं खेलता है।

कनेक्शन प्रतिक्रिया कोड "-1" है इसलिए यहां कुछ सुंदर बुरा चल रहा है। अपाचे एक्सेस या त्रुटि लॉग में कोई प्रविष्टियां दिखाई नहीं देती हैं, ऐसा लगता है जैसे अनुरोध एंड्रॉइड प्लेटफॉर्म से इसे कभी नहीं बना रहा है। कोड कनेक्शन लिखने के माध्यम से सही हो जाता है, लेकिन कनेक्शन पढ़ने, समय समाप्त होने और फिर लौटने पर लटकता है। असली फोन और एमुलेटर के लिए व्यवहार समान है।

क्या किसी को किसी भी गॉथस के बारे में पता है, जिसे एंड्रॉइड में मल्टीपार्ट फ़ाइल पोस्ट करते समय देखने की आवश्यकता है?

मैं (बनाया नाबालिग स्वच्छ mods) नीचे वर्ग आप देख सकते हैं तो क्या मैं

import java.io.ByteArrayOutputStream; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 

public class GeoPictureUploader 
{ 
    static String serviceDomain = "http://staging.abaqus.net"; 
    static String postUrl = serviceDomain + "/geo/upl/wupload/pictures"; 
    static String CRLF = "\r\n"; 
    static String twoHyphens = "--"; 
    static String boundary = "*****mgd*****"; 

    private String pictureFileName = null; 
    private String name = null; 
    private String password = null; 
    private DataOutputStream dataStream = null; 

    enum ReturnCode { noPicture, unknown, http201, http400, http401, http403, http404, http500}; 

    public GeoPictureUploader(String name, String password) 
    { 
     this.name = name; 
     this.password = password; 
    } 

    public static void setServiceDomain(String domainName) 
    { 
     serviceDomain = domainName; 
    } 

    public static String getServiceDomain() 
    { 
     return serviceDomain; 
    } 

    public ReturnCode uploadPicture(String pictureFileName) 
    { 
     this.pictureFileName = pictureFileName; 
     File uploadFile = new File(pictureFileName); 

     if (uploadFile.exists()) 
      try 
      { 
       FileInputStream fileInputStream = new FileInputStream(uploadFile); 
       URL connectURL = new URL(postUrl); 
       HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection(); 

       conn.setDoInput(true); 
       conn.setDoOutput(true); 
       conn.setUseCaches(false); 
       conn.setRequestMethod("POST"); 

       conn.setRequestProperty("User-Agent", "myGeodiary-V1"); 
       conn.setRequestProperty("Connection","Keep-Alive"); 
       conn.setRequestProperty("Content-Type","multipart/form-data;boundary="+boundary); 

       conn.connect(); 

       dataStream = new DataOutputStream(conn.getOutputStream()); 

       writeFormField("login", name); 
       writeFormField("password", password); 
       writeFileField("photo1", pictureFileName, "image/jpg", fileInputStream); 

       // final closing boundary line 
       dataStream.writeBytes(twoHyphens + boundary + twoHyphens + CRLF); 

       fileInputStream.close(); 
       dataStream.flush(); 
       dataStream.close(); 
       dataStream = null; 

       String response = getResponse(conn); 
       int responseCode = conn.getResponseCode(); 

       if (response.contains("uploaded successfully")) 
        return ReturnCode.http201; 
       else 
        // for now assume bad name/password 
        return ReturnCode.http401; 
      } 
      catch (MalformedURLException mue) { 
       // Log.e(Tag, "error: " + mue.getMessage(), mue); 
       System.out.println("GeoPictureUploader.uploadPicture: Malformed URL: " + mue.getMessage()); 
       return ReturnCode.http400; 
      } 
      catch (IOException ioe) { 
       // Log.e(Tag, "error: " + ioe.getMessage(), ioe); 
       System.out.println("GeoPictureUploader.uploadPicture: IOE: " + ioe.getMessage()); 
       return ReturnCode.http500; 
      } 
      catch (Exception e) { 
       // Log.e(Tag, "error: " + ioe.getMessage(), ioe); 
       System.out.println("GeoPictureUploader.uploadPicture: unknown: " + e.getMessage()); 
       return ReturnCode.unknown; 
      } 
     else 
     { 
      return ReturnCode.noPicture; 
     } 
    } 

    /** 
    * @param conn 
    * @return 
    */ 
    private String getResponse(HttpURLConnection conn) 
    { 
     try 
     { 
      DataInputStream dis = new DataInputStream(conn.getInputStream()); 
      byte []  data = new byte[1024]; 
      int    len = dis.read(data, 0, 1024); 

      dis.close(); 
      int responseCode = conn.getResponseCode(); 

      if (len > 0) 
       return new String(data, 0, len); 
      else 
       return ""; 
     } 
     catch(Exception e) 
     { 
      System.out.println("GeoPictureUploader: biffed it getting HTTPResponse"); 
      //Log.e(TAG, "GeoPictureUploader: biffed it getting HTTPResponse"); 
      return ""; 
     } 
    } 

    /** 
    * this mode of reading response no good either 
    */ 
    private String getResponseOrig(HttpURLConnection conn) 
    { 
     InputStream is = null; 
     try 
     { 
      is = conn.getInputStream(); 
      // scoop up the reply from the server 
      int ch; 
      StringBuffer sb = new StringBuffer(); 
      while((ch = is.read()) != -1) { 
       sb.append((char)ch); 
      } 
      return sb.toString(); // TODO Auto-generated method stub 
     } 
     catch(Exception e) 
     { 
      System.out.println("GeoPictureUploader: biffed it getting HTTPResponse"); 
      //Log.e(TAG, "GeoPictureUploader: biffed it getting HTTPResponse"); 
     } 
     finally 
     { 
      try { 
      if (is != null) 
       is.close(); 
      } catch (Exception e) {} 
     } 

     return ""; 
    } 

    /** 
    * write one form field to dataSream 
    * @param fieldName 
    * @param fieldValue 
    */ 
    private void writeFormField(String fieldName, String fieldValue) 
    { 
     try 
     { 
      dataStream.writeBytes(twoHyphens + boundary + CRLF);  
      dataStream.writeBytes("Content-Disposition: form-data; name=\"" + fieldName + "\"" + CRLF); 
      dataStream.writeBytes(CRLF); 
      dataStream.writeBytes(fieldValue); 
      dataStream.writeBytes(CRLF); 
     } 
     catch(Exception e) 
     { 
      System.out.println("GeoPictureUploader.writeFormField: got: " + e.getMessage()); 
      //Log.e(TAG, "GeoPictureUploader.writeFormField: got: " + e.getMessage()); 
     } 
    } 

    /** 
    * write one file field to dataSream 
    * @param fieldName - name of file field 
    * @param fieldValue - file name 
    * @param type - mime type 
    * @param fileInputStream - stream of bytes that get sent up 
    */ 
    private void writeFileField(
     String fieldName, 
     String fieldValue, 
     String type, 
     FileInputStream fis) 
    { 
     try 
     { 
      // opening boundary line 
      dataStream.writeBytes(twoHyphens + boundary + CRLF);  
      dataStream.writeBytes("Content-Disposition: form-data; name=\"" 
            + fieldName 
            + "\";filename=\"" 
            + fieldValue 
            + "\"" 
            + CRLF); 
      dataStream.writeBytes("Content-Type: " + type + CRLF); 
      dataStream.writeBytes(CRLF); 

      // create a buffer of maximum size 
      int bytesAvailable = fis.available(); 
      int maxBufferSize = 1024; 
      int bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      byte[] buffer = new byte[bufferSize]; 
      // read file and write it into form... 
      int bytesRead = fis.read(buffer, 0, bufferSize); 
      while (bytesRead > 0) 
      { 
       dataStream.write(buffer, 0, bufferSize); 
       bytesAvailable = fis.available(); 
       bufferSize = Math.min(bytesAvailable, maxBufferSize); 
       bytesRead = fis.read(buffer, 0, bufferSize); 
      } 

      // closing CRLF 
      dataStream.writeBytes(CRLF); 
     } 
     catch(Exception e) 
     { 
      System.out.println("GeoPictureUploader.writeFormField: got: " + e.getMessage()); 
      //Log.e(TAG, "GeoPictureUploader.writeFormField: got: " + e.getMessage()); 
     } 
    } 


    /** 
    * @param args 
    */ 
    public static void main(String[] args) 
    { 
     if (args.length >= 0) 
     { 
      GeoPictureUploader gpu = new GeoPictureUploader("john", "notmyrealpassword"); 
      String picName = args[0]; 

      ReturnCode rc = gpu.uploadPicture(picName); 
      System.out.printf("done"); 
     } 
    } 

} 
+0

डिबगिंग के कई घंटे के बाद, यह पता चला है हमारे रूटर में एक बग है कि किसी कारण से या एक अन्य एक आउटबाउंड राउंड ट्रिप पर वापस हमारे मचान में व्यतीत कर रहा है होना करने के लिए सर्वर जब संदेश कुछ के बाइट्स पर है। मैं एक हैंडसेट पर विकास कर रहा था जो वायरलेस नेटवर्क कनेक्शन का उपयोग कर रहा था और निश्चित रूप से एम्यूलेटर नेटवर्क का भी उपयोग करता है। एक बार जब हम अपने उत्पादन सर्वर को लक्षित करते हैं तो यह तुरंत काम करता है। सेलुलर भी अच्छा है। – jottos

+3

हर कोई इस एंड्रॉइड मल्टीपार्ट/फॉर्म-डेटा क्लास का उपयोग करने में संकोच न करें। यह एक चैंप की तरह काम करता है। और आसानी से एक सामान्य मल्टीपार्ट/फॉर्म-डेटा क्लास के लिए सामान्यीकृत किया जाता है। कोड पर सफाई संकेत - आप GetResponse() के पक्ष में तरीकों HttpURLConnection विधि conn.getResponseMethod(), और निश्चित रूप से conn.getResponseCode() उचित http सर्वर कोड – jottos

उत्तर

2

आप इंटरनेट अनुमति सेट है अप करने के लिए कर रहा हूँ शामिल कर रहा हूँ? मैं निश्चित रूप से पोस्ट को डीबग करने का प्रयास करने से पहले सरल गेट्स (HTTP लाने के लिए HTTPUrl का उपयोग करें) सुनिश्चित करता हूं।

+0

हाँ, आवेदन प्राप्त करने के लिए निकाल सकते हैं बहुत कुछ एचटीपी हो जाता है - सभी सफल – jottos

0

HI मैंने उसी HttpURLConnection का उपयोग करके फ़ाइल अपलोड किया है। मैं आरामदायक सेवाओं के लिए 25 एमबी तक फ़ाइलों को अपलोड करने में सक्षम हूं। एक नज़र यह आपके लिए उपयोगी हो सकता है है: link

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