2016-02-16 61 views
-4

我想了解接口如何工作。我已經在線閱讀了基本的界面教程,並觀看了一些視頻,所以我確實瞭解了一個界面和它的優點。界面是如何工作的?

接口

public interface UpyunFormApi { 
@Description("上傳文件") 
@POST("/{bucket}") 
@Multipart 
public Request upload(@Path("bucket") String bucket, @Part("policy") PolicyPart policy, 
     @Part("signature") SignaturePart signature, @Part("file") File file, OnRequestListener requestListener, 
     OnResponseListener<UpyunFormEntity> responseListener, OnErrorListener errorListener); 

代碼

private UpyunFormApi formApi; 
private void uploadAndPushTopic() { 
     String bucket = UrlManager.getInstance().getUpyunImageBucket(); 
     String secret = UrlManager.getInstance().getUpyunImageSecret(); 

     for (File file : filearr) { 
      PolicyPart policy = new PolicyPart(bucket); 
      SignaturePart signature = new SignaturePart(policy, secret); 
      formApi.upload(bucket, policy, signature, file, uploadRequestListener, uploadResponseListener, 
        uploadErrorListener); 
     } 
    } 

    private OnRequestListener uploadRequestListener = new OnRequestListener() { 

     @Override 
     public void onRequest(Request arg0) { 
     } 
    }; 

    private OnErrorListener uploadErrorListener = new OnErrorListener() { 

     @Override 
     public void onError(LegolasException arg0) { 
      } 
    }; 

    private OnResponseListener<UpyunFormEntity> uploadResponseListener = new OnResponseListener<UpyunFormEntity>() { 

     @Override 
     public void onResponse(UpyunFormEntity arg0) { 
      } 
     } 
    }; 

爲什麼Responselister工作後 「formApi.upload()」 完成了嗎?我找不到函數definition.Help! 我不明白的代碼

@Description("上傳文件") 
    @POST("/{bucket}") 
    @Multipart 
+5

「我已經閱讀了基本的界面教程......」現在閱讀您正在使用的註釋的文檔。 – ChiefTwoPencils

+0

'爲什麼Responselister在「formApi.upload()」完成之後工作? - 您是否檢查要拋出的異常或錯誤?數組中只有一個文件嗎?我建議你調試你的代碼,看看發生了什麼。 – Thomas

回答

0

作出這樣的接口:

public interface ChangeItemInterface { 

     public void doChange(String anyValue); 

    } 

適配器,

Intialize接口對象,如:

ChangeItemInterface changeItemInterface; 

在適配器構造函數,

this.changeItemInterface = context; 

適配器,從任何瀏覽點擊:

AnyView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       this.changeItemInterface.doChange("AnyValue"); 
       // It will go to the Your Activity Overided method which is explained below this 


      } 
     }); 

您的活動實現這個INTERF王牌,如:

 public class YourActivity extends Activity implements ChangeItemInterface{ 

     /// You'll get override method of your interface, here your call back will come when from adapter click happen 
     @Override 
      public void doChange(String anyValue) { 

       /// Here you can update any value in your activity ! 
      } 

     } 

希望這個演示幫助您瞭解接口使用!