无法通过 Android 中的改造从本地主机获取 JSON
Unable to fetch JSON fromlocal host with retrofit in Android
我正在尝试从本地主机获取 JSON 但无法这样做。
我可以通过在虚拟设备的浏览器中键入 URL (http://10.0.2.2/evapo/json_get_data.php) 来访问该文件,但不知何故无法从代码中访问它。
主要ActivityClass
class MainActivity : AppCompatActivity()
{
private val CROP_BASE_URL="http://10.0.2.2/"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
makeAPIRequest()
}
fun makeAPIRequest()
{
val api:APIRequest =Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(APIRequest::class.java)
GlobalScope.launch {
try {
Log.d("TEST", "makeAPIRequest: HERE")
val response:ServerResponse=api.getCropData()
//control never reach here
Log.d("TEST", "makeAPIRequest:"+response)
}catch (e:Exception)
{
e.printStackTrace()
}
}
}
}
API 请求接口
interface APIRequest {
@GET("evapo/json_get_data.php")
suspend fun getCropData():ServerResponse
}
裁剪反应Class
data class CropResponse(
@SerializedName("server_response")
val serverResponse: List<ServerResponse>
)
服务器响应Class
data class ServerResponse(
@SerializedName("cropName")
val cropName: String,
@SerializedName("eigth_month")
val eigthMonth: String,
@SerializedName("eleventh_month")
val eleventhMonth: String,
@SerializedName("fifth_month")
val fifthMonth: String,
@SerializedName("first_month")
val firstMonth: String,
@SerializedName("fourth_month")
val fourthMonth: String,
@SerializedName("nineth_month")
val ninethMonth: String,
@SerializedName("second_month")
val secondMonth: String,
@SerializedName("seventh_month")
val seventhMonth: String,
@SerializedName("sixth_month")
val sixthMonth: String,
@SerializedName("sowingDate")
val sowingDate: String,
@SerializedName("tenth_month")
val tenthMonth: String,
@SerializedName("third_month")
val thirdMonth: String,
@SerializedName("twelveth_month")
val twelvethMonth: String
)
json_get_data.phpreturns
{
"server_response": [
{
"cropName": "Cotton",
"sowingDate": "03-03-2020",
"first_month": "85.59",
"second_month": "185.134",
"third_month": "261.88",
"fourth_month": "388.608",
"fifth_month": "312.979",
"sixth_month": "219.848",
"seventh_month": "193",
"eigth_month": "0",
"nineth_month": "0",
"tenth_month": "0",
"eleventh_month": "0",
"twelveth_month": "0"
}
]
}
登录猫
2020-09-01 13:10:06.608 10803-10828/dummy.dummy D/TEST: makeAPIRequest: HERE
正在从 log cat 链接堆栈跟踪,因为已达到字符数限制
Stack trace
解决方案
- 更改了已接受答案中指定的 return 类型。
- 向 baseURL()
传递错误的 URL 而不是“http://10.0.2.2/”
由于您的 API return 是一个列表,您需要 return
suspend fun getCropData(): CropResponse
在API请求接口
由于您的 api 是 return 列表,因此您需要 return CropResponse
包装在 Resonse
中的 Retrofit 对象中14=]函数如下
interface APIRequest {
@GET("evapo/json_get_data.php")
suspend fun getCropData() : Response<CropResponse>
}
然后,在协程中你得到如下数据:
GlobalScope.launch {
try {
Log.d("TEST", "makeAPIRequest: HERE")
val response: CropResponse = api.getCropData().body()
val serverResponse: List<ServerResponse> = response.serverResponse
Log.d("TEST", "makeAPIRequest:"+response)
}catch (e:Exception)
{
e.printStackTrace()
}
}
我正在尝试从本地主机获取 JSON 但无法这样做。 我可以通过在虚拟设备的浏览器中键入 URL (http://10.0.2.2/evapo/json_get_data.php) 来访问该文件,但不知何故无法从代码中访问它。
主要ActivityClass
class MainActivity : AppCompatActivity()
{
private val CROP_BASE_URL="http://10.0.2.2/"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
makeAPIRequest()
}
fun makeAPIRequest()
{
val api:APIRequest =Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(APIRequest::class.java)
GlobalScope.launch {
try {
Log.d("TEST", "makeAPIRequest: HERE")
val response:ServerResponse=api.getCropData()
//control never reach here
Log.d("TEST", "makeAPIRequest:"+response)
}catch (e:Exception)
{
e.printStackTrace()
}
}
}
}
API 请求接口
interface APIRequest {
@GET("evapo/json_get_data.php")
suspend fun getCropData():ServerResponse
}
裁剪反应Class
data class CropResponse(
@SerializedName("server_response")
val serverResponse: List<ServerResponse>
)
服务器响应Class
data class ServerResponse(
@SerializedName("cropName")
val cropName: String,
@SerializedName("eigth_month")
val eigthMonth: String,
@SerializedName("eleventh_month")
val eleventhMonth: String,
@SerializedName("fifth_month")
val fifthMonth: String,
@SerializedName("first_month")
val firstMonth: String,
@SerializedName("fourth_month")
val fourthMonth: String,
@SerializedName("nineth_month")
val ninethMonth: String,
@SerializedName("second_month")
val secondMonth: String,
@SerializedName("seventh_month")
val seventhMonth: String,
@SerializedName("sixth_month")
val sixthMonth: String,
@SerializedName("sowingDate")
val sowingDate: String,
@SerializedName("tenth_month")
val tenthMonth: String,
@SerializedName("third_month")
val thirdMonth: String,
@SerializedName("twelveth_month")
val twelvethMonth: String
)
json_get_data.phpreturns
{
"server_response": [
{
"cropName": "Cotton",
"sowingDate": "03-03-2020",
"first_month": "85.59",
"second_month": "185.134",
"third_month": "261.88",
"fourth_month": "388.608",
"fifth_month": "312.979",
"sixth_month": "219.848",
"seventh_month": "193",
"eigth_month": "0",
"nineth_month": "0",
"tenth_month": "0",
"eleventh_month": "0",
"twelveth_month": "0"
}
]
}
登录猫
2020-09-01 13:10:06.608 10803-10828/dummy.dummy D/TEST: makeAPIRequest: HERE
正在从 log cat 链接堆栈跟踪,因为已达到字符数限制 Stack trace
解决方案
- 更改了已接受答案中指定的 return 类型。
- 向 baseURL() 传递错误的 URL 而不是“http://10.0.2.2/”
由于您的 API return 是一个列表,您需要 return
suspend fun getCropData(): CropResponse
在API请求接口
由于您的 api 是 return 列表,因此您需要 return CropResponse
包装在 Resonse
中的 Retrofit 对象中14=]函数如下
interface APIRequest {
@GET("evapo/json_get_data.php")
suspend fun getCropData() : Response<CropResponse>
}
然后,在协程中你得到如下数据:
GlobalScope.launch {
try {
Log.d("TEST", "makeAPIRequest: HERE")
val response: CropResponse = api.getCropData().body()
val serverResponse: List<ServerResponse> = response.serverResponse
Log.d("TEST", "makeAPIRequest:"+response)
}catch (e:Exception)
{
e.printStackTrace()
}
}