我需要帮助来弄清楚如何解决我的 Activity 中两个相互冲突的导入的问题,即:
retrofit2.Response
com.android.volley.Response
第一个导入是为了帮助我管理 api 回调响应,而第二个是为了帮助我在上传文件时管理 stringrequest 响应。单独实现一个,即仅运行 api 调用来获取数据,它自己就可以正常工作,就像自己上传文件一样。现在将两者合并为一个 Activity ,这样当用户从下拉列表中选择数据(其列表来自服务器)并随后上传文件时,事实证明获取错误非常麻烦:
retrofit2.Response
already defined in single type import
或
com.android.volley.Response
already defined in single type import
取决于哪个先到。
最佳答案
在 Java 中,您不能导入两个具有相同名称的不同类。在这些情况下,您可以导入一个,但使用具有完全限定名称的另一个。
例如:
import retrofit2.Response;
Response response = ...; //This is Retrofit Response object
com.android.volley.Response otherResponse = ...; //This is volley Response object
编辑:
在 Kotlin 中,您可以通过分配别名来导入
import retrofit2.Response as RResponse
import com.android.volley.Response as VResponse
val response: RResponse = ... //This is Retrofit Response object
val otherResponse: VResponse = ... //This is volley Response object
关于android - 导入已在单一类型导入中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56205609/