我正在开发一个需要从本地 android assets directory 访问Json数据的项目 . 我可以使用 Volley Library 从assets文件夹中读取json数据,但我想使用 Retrofit 做同样的事情 . 下面是我的json文件 - myfile.json
.
{
"formules": [
{
"formule": "Linear Motion",
"url": "<html><body>You scored <b>192</b> points. <img src=\"http://192.168.1.46/shanta_holdings/img/images/project-thumbs/Glasshouse-1.jpg\" alt=\"Smiley face\"/></body></html> ",
"des":"Here is a description"
},
{
"formule": "Constant Acceleration Motion",
"url": "<html><body>You scored <b>192</b> points. <img src=\"http://192.168.1.46/shanta_holdings/img/images/project-thumbs/Glasshouse-1.jpg\" alt=\"Smiley face\"/></body></html> ",
"des":"Here is a description"
},
{
"formule": "Projectile Motion",
"url": "<html><body>You scored <b>192</b> points. <img src=\"http://192.168.1.46/shanta_holdings/img/images/project-thumbs/Glasshouse-1.jpg\" alt=\"Smiley face\"/></body></html> ",
"des":"Here is a description"
},
{
"formule": "Force",
"url": "<html><body>You scored <b>192</b> points. <img src=\"http://192.168.1.46/shanta_holdings/img/images/project-thumbs/Glasshouse-1.jpg\" alt=\"Smiley face\"/></body></html> ",
"des":"Here is a description"
},
{
"formule": "Work, Power, Energy",
"url": "<html><body>You scored <b>192</b> points. <img src=\"http://192.168.1.46/shanta_holdings/img/images/project-thumbs/Glasshouse-1.jpg\" alt=\"Smiley face\"/></body></html> ",
"des":"Here is a description"
},
{
"formule": "Rotary Motion",
"url": "<html><body>You scored <b>192</b> points. <img src=\"http://192.168.1.46/shanta_holdings/img/images/project-thumbs/Glasshouse-1.jpg\" alt=\"Smiley face\"/></body></html> ",
"des":"Here is a description"
},
{
"formule": "Harmonic Motion",
"url": "<html><body>You scored <b>192</b> points. <img src=\"http://192.168.1.46/shanta_holdings/img/images/project-thumbs/Glasshouse-1.jpg\" alt=\"Smiley face\"/></body></html> ",
"des":"Here is a description"
},
{
"formule": "Gravity",
"url": "<html><body>You scored <b>192</b> points. <img src=\"http://192.168.1.46/shanta_holdings/img/images/project-thumbs/Glasshouse-1.jpg\" alt=\"Smiley face\"/></body></html> ",
"des":"Here is a description"
},
{
"formule": "Lateral and Longitudinal Waves",
"url": "<html><body>You scored <b>192</b> points. <img src=\"http://192.168.1.46/shanta_holdings/img/images/project-thumbs/Glasshouse-1.jpg\" alt=\"Smiley face\"/></body></html> ",
"des":"Here is a description"
},
{
"formule": "Sound Waves",
"url": "<html><body>You scored <b>192</b> points. <img src=\"http://192.168.1.46/shanta_holdings/img/images/project-thumbs/Glasshouse-1.jpg\" alt=\"Smiley face\"/></body></html> ",
"des":"Here is a description"
},
{
"formule": "Electrostatics",
"url": "<html><body>You scored <b>192</b> points. <img src=\"http://192.168.1.46/shanta_holdings/img/images/project-thumbs/Glasshouse-1.jpg\" alt=\"Smiley face\"/></body></html> ",
"des":"Here is a description"
},
{
"formule": "Direct Current",
"url": "<html><body>You scored <b>192</b> points. <img src=\"http://192.168.1.46/shanta_holdings/img/images/project-thumbs/Glasshouse-1.jpg\" alt=\"Smiley face\"/></body></html> ",
"des":"Here is a description"
},
{
"formule": "Magnetic Field",
"url": "https://www.google.com/",
"des":"Here is a description"
},
{
"formule": "Alternating Current",
"url": "https://www.google.com/",
"des":"Here is a description"
},
{
"formule": "Thermodynamics",
"url": "https://www.google.com/",
"des":"Here is a description"
},
{
"formule": "Hydrogen Atom",
"url": "https://www.google.com/",
"des":"Here is a description"
},
{
"formule": "Optics",
"url": "https://www.google.com/",
"des":"Here is a description"
},
{
"formule": "Modern Physics",
"url": "https://www.google.com/",
"des":"Here is a description"
},
{
"formule": "Hydrostatics",
"url": "https://www.google.com/",
"des":"Here is a description"
},
{
"formule": "Astronomy",
"url": "https://www.google.com/",
"des":"Here is a description"
}
]
}
我已经搜索了一个星期,但没有得到任何适当的改造解决方案 . 我可以使用Retrofit从服务器读取数据 . 有没有办法通过改造来达到要求 .
2 回答
正如Dheerubhai所写,Retrofit不用于解析JSON对象 . 您可以使用GSON,请参阅this thread以了解如何使其与Retrofit一起使用,您可以看到this tutorial .
您正在尝试在项目中实现存根 .
Add your .json file into res/raw/filename.json
上面的示例显示了两个Web服务 endpoints 的本地存根文件 - / some / web / service,/ another / web / service)和两个方案 - empty和default(其中默认存根没有前缀,空存根以“empty_”为前缀) ) . 这些文件的内容通常是JSON .
接下来,我们编写一个自定义HTTP客户端来从这些本地文件而不是网络中获取 . 以下示例从Square的Retrofit库实现Client对象 . 如果您不熟悉Retrofit,则默认的Client实现是OkClient . 它生成标准的HTTP网络请求 . 我们正在编写一个自定义客户端来伪造网络请求并从本地文件而不是网络服务器获取响应数据 .
上面的示例显示了两个Web服务 endpoints 的本地存根文件 - / some / web / service,/ another / web / service)和两个方案 - empty和default(其中默认存根没有前缀,空存根以“empty_”为前缀) ) . 这些文件的内容通常是JSON .
接下来,我们编写一个自定义HTTP客户端来从这些本地文件而不是网络中获取 . 以下示例从Square的Retrofit库实现Client对象 . 如果您不熟悉Retrofit,则默认的Client实现是OkClient . 它生成标准的HTTP网络请求 . 我们正在编写一个自定义客户端来伪造网络请求并从本地文件而不是网络服务器获取响应数据 .
Finally we check our “Backend Environment” setting to determine if we should use our LocalStubClient or the default OkClient:
模拟网络延迟和故障Retrofit还允许我们模拟网络延迟,差异和故障 . 这是基于我们的第二个Dev设置启用/禁用模拟网络适配器的代码: