Android应用程序制作(是否有可能利用github页面?)
我正在制作一个简单的android应用程序。为了描述它,它显示了一系列CPU型号,如i7、i5和AMD处理器等 我不熟悉使用数据库!所以我想写txt文件并将其存储在我的GitHub主页上。然后,是否可以加载txt文件并在android应用程序中显示它?是的,您可以。 您必须创建一个Android应用程序制作(是否有可能利用github页面?),android,database,github,Android,Database,Github,我正在制作一个简单的android应用程序。为了描述它,它显示了一系列CPU型号,如i7、i5和AMD处理器等 我不熟悉使用数据库!所以我想写txt文件并将其存储在我的GitHub主页上。然后,是否可以加载txt文件并在android应用程序中显示它?是的,您可以。 您必须创建一个公共存储库,并在该存储库中将文本文件存储为JSON文件 注意:存储库必须是公共的 然后,您可以使用如下所示的任何httpclient以API URL的形式访问此文件 val client = OkHttpClient(
公共存储库
,并在该存储库中将文本文件存储为JSON文件
注意:存储库必须是公共的
然后,您可以使用如下所示的任何httpclient
以API URL
的形式访问此文件
val client = OkHttpClient() // have to add OkHttpClient in gradle file.
val request = Request.Builder()
.url("https://gitlab.com/jakir123/personaldictionary/-/raw/master/app_info.json") // the file link don't forget to replace blob with raw.
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
EasyLog.logE(
"Exception in getting app info: ${e.localizedMessage}",
"AppInfoRepository"
)
}
override fun onResponse(call: Call, response: Response) {
val body = response.body()?.string()
val gson = GsonBuilder().create()
val appInfo = gson.fromJson(body, AppInfo::class.java)
// here you can access value like below
val versionCode = appInfo.version_code
val versionName = appInfo.version_name
}
})
AppInfo模型类
data class AppInfo(
val version_code: Int,
val version_name: String,
val developer_name: String,
val developer_email: String,
val developer_image: String,
val playstore_link: String
)
public class AppInfo{
private int version_code;
private String version_name;
private String developer_name;
private String developer_email;
private String developer_image;
private String playstore_link;
//getters and setters
}
更新:Java示例代码
OkHttpClient client = new OkHttpClient();
// GET request
Request request = new Request.Builder()
.url("your_url")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
// handle on failure here.
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
} else {
// do something wih the response
String result = response.body().toString() // you will get your json text here as string.
Gson gson = new Gson(); // this library should be added in gradle
AppInfo appInfo= gson.fromJson(result, AppInfo.class);
// here you can access value like below
int versionCode = appInfo.version_code
String versionName = appInfo.version_name
}
}
});
AppInfo模型类
data class AppInfo(
val version_code: Int,
val version_name: String,
val developer_name: String,
val developer_email: String,
val developer_image: String,
val playstore_link: String
)
public class AppInfo{
private int version_code;
private String version_name;
private String developer_name;
private String developer_email;
private String developer_image;
private String playstore_link;
//getters and setters
}
您是创建文件txt并共享到GitHub,还是从GitHub获取文件并读取文件(在文本视图中显示)??这正是我要说的!因为我认为它可以为制作服务器节省一些钱哦,你真是太好了!谢谢你,侯赛因。我想是科特林,对吗?“我会试试的。”杰克·侯赛因我很高兴你的帮助。。。。您可以用Java语言重写代码吗?