2012-08-28 5 views
0

내 안드로이드 휴대 전화에서 서버 URL로 파일을 업로드하고 싶습니다. 하지만 서버가 먼저 로그인해야했습니다. 로그인 URL과 함께이 서버 URL에 파일을 업로드하려면 어떻게해야합니까? 예를 들어 : 업로드 URL : 필요 http://api.someapi.net/v1/medium/14 매개 변수 : 파일 제목android의 서버에 파일 업로드

로그인 URL : 필요 http://api.someapi.net/v1/user/login 매개 변수 : 이메일과 비밀번호 당신은 (여기 프로젝트에 아파치의 Mime4J jar 파일이 필요

+0

을 도움이되기를 바랍니다. 그런 다음 reply (token, file, title)가 필요합니다. – mario

답변

0

아래 코드를 사용하여 로그인 자격 증명을 확인하십시오. 응답이 OK이면 다른 매개 변수로 아래의 동일한 코드를 사용하여 url에 파일을 보내십시오. 원하는 건 뭐든 할거야.

URL siteUrl; 
    try { 
     siteUrl = new URL("yoururl"); 
     HttpURLConnection conn = (HttpURLConnection) siteUrl 
       .openConnection(); 
     conn.setRequestMethod("POST"); 
     conn.setDoOutput(true); 
     conn.setDoInput(true); 

     DataOutputStream out = new DataOutputStream(conn.getOutputStream()); 
     String content1 = ""; 

     //Here param is the Map<String,String> which holds the value of email and password 
     Set getkey = param.keySet(); 
     Iterator keyIter = getkey.iterator(); 
     String content = ""; 
     for (int i = 0; keyIter.hasNext(); i++) { 
      Object key = keyIter.next(); 
      if (i != 0) { 
       content += "&"; 
      } 
      content += key + "=" + param.get(key); 

     } 
     //Check by printing here 
     System.out.println(content); 
     out.writeBytes(content.trim()); 
     out.flush(); 
     out.close(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(
       conn.getInputStream())); 
     String line = ""; 
     while ((line = in.readLine()) != null) { 
      System.out.println(line); 
     } 
     in.close(); 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 

은 로그인 생성하고 토큰을 반환 할 수 있습니다, 당신은 사용자가 처음 로그인되어 있는지 확인하려면이

0

-http://james.apache.org/download.cgi#Apache_Mime4J)

그리고는 수행

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
entity.addPart("file", new InputStreamBody(file, "UPLOAD.jpg")); 
request.setEntity(entity); 

타다!

+0

답장을 보내 주셔서 감사합니다. 나는 지금 노력하고있다 ..... –

+0

덕분에 작동한다. –