用android更新数据库上的一个属性

时间:2017-07-31 04:52:23

标签: android cakephp

我正在使用后端的cakephp开发一个Android应用程序。我有一个包含20个属性的数据库表,比如xx。我已经开发了相关的rest api。使用邮递员一切都很完美。在android上我有一个问题!

如果我只想为xx表更新一条记录的一个属性,该怎么办?从android,我应该再次发布所有数据吗?如果不是,我如何使用AsyncTask中的记录的id来仅更新该属性?

更新

这是我的代码示例:

public class AsycTaskManager {

    public static class AddFlightPlan extends AsyncTask<String, Void, String> {
        String result ;
        Car car = new Car();
        String token ;
        @Override
        protected String doInBackground(String... params) {
            car = params[0];
            token = params[1];
            String editCar = "http://xxx.xxx.xxx.xxx:xxxx/backend/0/cars/"+car.id+"/editcar";

            URL object = null;
            try {
                object = new URL(editCar);
                HttpURLConnection con = (HttpURLConnection) object.openConnection();
                con.setDoOutput(true);
                con.setDoInput(true);
                con.setRequestProperty("Accept", "application/json");
                con.setRequestProperty("Content-type", "application/json");
                con.setRequestProperty("Authorization", token);
                con.setRequestMethod("PATCH");
               JSONObject cred = new JSONObject();
               cred.put("name", car.name);
               cred.put("description",car.desc);
               cred.put("code", car.code);
               cred.put("date", car.date);
               cred.put("guid", car.guid);
               cred.put("baggage_code", car.bag_code);
               cred.put("baggage_weight",car.bag_weight);
               cred.put("max_speed", car.max_speed);

                OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
                wr.write(cred.toString());
                wr.flush();
                StringBuilder sb = new StringBuilder();
                int HttpResult = con.getResponseCode();
                if (HttpResult == HttpURLConnection.HTTP_OK) {
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(con.getInputStream(), "utf-8"));
                    String line = null;
                    while ((line = br.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    br.close();
                   JSONObject jObj = new JSONObject(sb.toString());
                    Log.e("result","f"+sb.toString());
                    if (sb.toString() != null) {
                        return sb.toString();
                    }
                } else {
                    System.out.println(con.getResponseMessage());
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }


        @Override
        protected void onPostExecute(String result) {
            Log.e("result","null "+result);
        }
    }
}

例如,我想只更新名称

0 个答案:

没有答案