I have a simple json file located on server path "myserver/samplejsontotest.json"
content of a json file is:
{"apps": [{"appName": "App1", "version": 1.0}, {"appName": "App2", "version": 4.6}]}
I want to load the json file, parse and read it's content and show App names when I click a button on Main activity. Below is my code in MainActivity.java:
public class MainActivity extends AppCompatActivity {
String path = "myserver/samplejsontotest.json";
JSONObject reader = new JSONObject(path);
public MainActivity() throws JSONException {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Log.d("Button1", "Clicked on Button1");
}
});
}
public void parseJson() throws JSONException {
JSONObject sys = reader.getJSONObject("apps");
String app = sys.getString("appName");
String version = sys.getString("version");
}
}
Please help.
Try the Rest Template of Spring.
Just create a java class that represents the json structure, and consume the json file as a GET method.
Reference: https://docs.spring.io/spring-android/docs/1.0.0.M1/reference/html/rest-template.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.