Java Android studio - 向API添加新条目

时间:2018-04-26 21:35:05

标签: java android api

需要一些帮助,了解如何与我提供的API建立连接以添加/更新/删除条目,但我想一旦我理解添加,我就能够想到除了另外两个。

我已经创建了一个可以编辑的纯文本字段的新活动,但显然我不知道如何添加其功能以在网站上添加该条目。

以下是API文档: 获取所有学生[GET]返回JSON数组中的所有学生。 PARAM 1 apikey - 给你的apikey。 网址http://radikaldesign.co.uk/sandbox/studentapi/getallstudents.php?apikey=YOUR_APIKEY

获取一名学生[GET]将单个学生作为JSON对象返回。 PARAM 1 apikey - 给你的apikey。 PARAM 2 studentnumber - 您要检索的学生的学号。 网址http://radikaldesign.co.uk/sandbox/studentapi/getstudent.php?apikey=YOUR_APIKEY&studentnumber=A_STUDENTNUMBER

添加学生[POST]将学生添加到数据库。 PARAM 1 apikey - 给你的apikey。 PARAM 2 json - 包含新学生的所有键值对的JSON对象。 网址http://radikaldesign.co.uk/sandbox/studentapi/add.php

更新学生[POST]更新现有学生。 PARAM 1 apikey - 给你的apikey。 PARAM 2 json - 包含学生的所有键值对的JSON对象。 网址http://radikaldesign.co.uk/sandbox/studentapi/update.php

删除学生[POST]删除现有学生。 PARAM 1 apikey - 给你的apikey。 PARAM 2 studentnumber - 您要删除的学生的学号。 网址http://radikaldesign.co.uk/sandbox/studentapi/delete.php

这是我的主要活动:

public class MainActivity extends AppCompatActivity {

String [] personNames;
ArrayList<Person> allPerson = new ArrayList<>();

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

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    ListView personList = (ListView) findViewById(R.id.personList);

    //make the button open a new activity with the form to add a new student
    Button btn = (Button)findViewById(R.id.buttonAdd);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, AddNewStudent.class));
        }
    });

    //http call
    HttpURLConnection urlConnection;
    InputStream in = null;
    try {
        URL url = new URL("http://radikaldesign.co.uk/sandbox/studentapi/getallstudents.php?apikey=xxx");
        urlConnection = (HttpURLConnection) url.openConnection();
        in = new BufferedInputStream(urlConnection.getInputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
    String response = convertStreamToString(in);
    System.out.println("Server response = " + response);

    try {
        // declare a new json array and pass it the string response from the server
        // this will convert the string into a JSON array which we can then iterate
        // over using a loop
        JSONArray jsonArray = new JSONArray(response);
        // instantiate the cheeseNames array and set the size
        // to the amount of cheese object returned by the server
        personNames = new String[jsonArray.length()];

        // use a for loop to iterate over the JSON array
        for (int i=0; i < jsonArray.length(); i++)
        {
            // the following line of code will get the name of the cheese from the
            // current JSON object and store it in a string variable called name
            String name = jsonArray.getJSONObject(i).get("name").toString();
            String gender = jsonArray.getJSONObject(i).get("gender").toString();
            String dob = jsonArray.getJSONObject(i).get("dob").toString();
            String address = jsonArray.getJSONObject(i).get("address").toString();
            String postcode = jsonArray.getJSONObject(i).get("postcode").toString();
            String studentnumber = jsonArray.getJSONObject(i).get("studentNumber").toString();
            String coursetitle = jsonArray.getJSONObject(i).get("courseTitle").toString();
            String startdate = jsonArray.getJSONObject(i).get("startDate").toString();
            String bursary = jsonArray.getJSONObject(i).get("bursary").toString();
            String email = jsonArray.getJSONObject(i).get("email").toString();
            // print to log cat
            System.out.println("name = " + name);

            Person c = new Person(name, gender, dob, address, postcode, studentnumber, coursetitle, startdate, bursary, email);
            allPerson.add(c);
            // add the name of the current cheese to the cheeseNames array
            personNames [i] = name;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, personNames);
    personList.setAdapter(arrayAdapter);

    personList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long
                l) {
            Toast.makeText(MainActivity.this, "selected " +
                    allPerson.get(i).getName(), Toast.LENGTH_SHORT).show();
            //declaring intent and sending it to the page you click on
            Intent intent = new Intent(getApplicationContext(), DetailsActivity.class);
            intent.putExtra("person", allPerson.get(i));
            startActivity(intent);
        }
    });

}

public String convertStreamToString(InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

}

仍然无法弄清楚这一点或找到一种方法, 任何帮助将不胜感激,谢谢

0 个答案:

没有答案