繁体   English   中英

在Android应用上显示在线服务器文件的内容

[英]Show content of online server file on android app

我目前正在开发一个应用,用户可以从不同主题的列表视图中访问研究材料。

在从中获取主题名称的数据库表中,还有一列,该列具有指向我的在线服务器上的文件夹的文件路径。 每个主题在服务器上都有一个单独的文件夹。

我该如何添加代码,以便当用户单击列表中的主题时,其特定文件夹的内容显示在新活动中?

这是我在简单列表视图中的代码:

 public class FindMaterialActivity extends Activity {

private ListView list;
private ArrayList<String> subjects;
private JSONArray result;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_find_material2);

    list = (ListView) findViewById(R.id.list);

    subjects = new ArrayList<String>();

    getData();

}

private void getData() {
    //Creating a string request
    StringRequest stringRequest = new StringRequest(SubConfig.DATA_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject j = null;
                    try {
                        //Parsing the fetched Json String to JSON Object
                        j = new JSONObject(response);

                        //Storing the Array of JSON String to our JSON Array
                        result = j.getJSONArray(SubConfig.JSON_ARRAY);

                        //Calling method getStudents to get the students from the JSON Array
                        getSubjects(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    //Creating a request queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //Adding request to the queue
    requestQueue.add(stringRequest);
}

private void getSubjects(JSONArray j) {
    //Traversing through all the items in the json array
    for (int i = 0; i < j.length(); i++) {
        try {
            //Getting json object
            JSONObject json = j.getJSONObject(i);

            //Adding the name of the student to array list
            subjects.add(json.getString(SubConfig.TAG_SUBJECTS));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //Setting adapter to show the items in the spinner
    list.setAdapter(new ArrayAdapter<String>(FindMaterialActivity.this, android.R.layout.simple_list_item_1, subjects));
}
}

和主题config.java

  public class SubConfig {

    //JSON URL
    public static final String DATA_URL = "http://opuna.co.uk/subject_api/FetchSub.php";

    //Tags used in the JSON String
    public static final String TAG_SUBJECTS = "Subject_Name";
    public static final String TAG_SUB_ID = "Sub_id";


    //JSON array name
    public static final String JSON_ARRAY = "result";
}

像处理主题一样,将有关子文件夹的元数据存储在数据库中,并像处理主题一样在应用程序中使用JSON获取元数据,并以自己的自定义布局显示它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM