2015-06-04 86 views
-2

如何通過電腦或相機在服務器上傳圖像?通過Httppost將圖像上傳到服務器

我嘗試this link的代碼,但它顯示了這樣的

Error:(329, 24) error: cannot access AbstractBody class file for org.apache.james.mime4j.message.AbstractBody not found

+0

分享您的code.What你有嘗試到現在? – Ravi

+0

(http://curious-blog.blogspot.in/2013/06/pick-image-and-upload-to-server-in.html)檢查這個鏈接我試試這個方法。 bt它顯示錯誤@GeorgeThomas – RushDroid

回答

0

您可以上傳圖像到服務器使用http後多部分。這裏是幫助你的鏈接。

https://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/

http://www.codejava.net/java-se/networking/upload-files-by-sending-multipart-request-programmatically

還有一方式來上傳圖片,將其轉換爲Base64和比上傳。檢查下面的鏈接。

Android upload image to server using base64

+0

謝謝你,但它仍然顯示此錯誤@Vid – RushDroid

+0

錯誤:(591,22)錯誤:無法訪問AbstractBody org.apache.james.mime4j.message.AbstractBody的未找到的類文件 注意:使用-Xlint:deprecation重新編譯以獲取詳細信息。 注意:某些輸入文件使用或覆蓋棄用的API。 注意:使用-Xlint重新編譯:取消選中以獲取詳細信息。這個錯誤我主要面對@Vid – RushDroid

0

錯誤,如果你使用異步HTTP作爲你的HTTP客戶端click-here那麼你可以用下面的方法[如果你選擇異步HTTP你可以使用此方法] 此方法不僅可以上傳圖像,還可以用於對服務器進行普通的後調用。

YourFragment.class [這個類中調用http請求]

public class YourFragment extends CommanAbstract 
    { 


    @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 

      View v = inflater.inflate(R.layout.your_layout, container, false); 
      ButterKnife.inject(this, v); 

      return v; 
     } 


    private void linkCall() { 
      // TODO Auto-generated method stub 

      RequestParams params = new RequestParams(); 
      params.put("params1", "value1"); 
      params.put("params2", "value2"); 
//note that "image" tag will be the tag which accepts the file to the server 
      params.put("image", new File("your image path from gallery or camera")); 
      parse(params, 1, your link here, true); 

     } 
    @Override 
     public void parseresult(String response, boolean success, int value) { 
      // TODO Auto-generated method stub 

      switch (value) { 
      case 1: 
       //here you can parse the link response 
       break; 
      } 
     } 

    @Override 
     public void error(String response) { 
      // TODO Auto-generated method stub 
      //error will come here 
     } 

    } 

CommanAbstract類[擴展該類中不斷類,你有一個http請求]

public abstract class CommanAbstract extends Fragment{ 

private static final String Url = "your link head here"; 

    public abstract void parseresult(String response, boolean success, int value); 

    public abstract void error(String response); 

    public void parse(RequestParams params, final int value, String link, 
      boolean progrss) { 
     // TODO Auto-generated method 

     AsyncHttpClient client = new AsyncHttpClient(); 
     GlobalFunctions.postApiCall(getActivity(), link, params, client, 
       new HttpResponseHandler() { 

        @Override 
        public void handle(String response, boolean success) { 
         // TODO Auto-generated method stub 

         if (success) { 
          parseresult(response, true, value); 
         } else { 
          error(response); 
          toast("Connection error"); 
         } 
        } 
       }); 
    } 
} 

GlobalFunctions類[此類呼叫http請求]

public class GlobalFunctions { 
    // static ProgressDialog progress; 

    public interface HttpResponseHandler { 
     void handle(String response,boolean failre); 
    } 


    public static void postApiCall(final Context context, final String url, 
      RequestParams params, AsyncHttpClient httpClient, 
      final HttpResponseHandler handler) { 

     httpClient.post(url, params, new AsyncHttpResponseHandler() { 
      @Override 
      public void onFailure(Throwable arg0, String failureResponse) { 
       // TODO Auto-generated method stub 

       super.onFailure(arg0, failureResponse); 
       System.out.println("fail" + failureResponse + "url is" + url); 
       handler.handle(failureResponse,false); 

       // errorToast(context); 
      } 

      @Override 
      public void onSuccess(String response) { 



       handler.handle(response,true); 

      } 
     }); 
    } 

} 
相關問題