如何在Android上通过json进行更新或“PUT”?

时间:2017-07-10 19:20:16

标签: java android json

  

我需要更新数据库中的现有参数。   我的屏幕接收xml中的值,它是我想使用put方法在数据库中更新的值。

     

这是我的put代码无法正常工作:

      public class PatchClass extends AsyncTask<Void, String, String> {

            protected void onPreExecute() {
            }


            @Override
            protected String doInBackground(Void... String) {

            StringBuilder result = new StringBuilder();
            //  Toast.makeText(LoginActivity.this,"No começo de doInbackground....",Toast.LENGTH_LONG).show();

            //   int id = 1;

              try {


                    String urlLogin = "http://192.168.1.207/api/v2/bookdemo/_table/cad_users";
                    URL url = new URL(urlLogin);

                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                    JSONObject postDataParams = new JSONObject();
                    conn.setRequestProperty("Content-type", "application/json");
                    conn.setRequestProperty("Accept", "application/json");
                    conn.setRequestProperty("X-DreamFactory-Api-Key", "XXXXXXXXXXXXXXXX");
                    conn.setRequestProperty("X-DreamFactory-Session-Token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.XXXXXXXXXXXXXX.uVXdVTczx91z2NDGVV2quxvIEHhHUzX1hnVrNWQzoao");
                    conn.setRequestProperty("Authorization", "Basic dGhpYWdvLmNhbWFyZ29AZXZvbHV0aW9uaXQuY29tLmJyOmluaWNpYWwyMDE3'");
                    conn.setDoOutput(true);
                    conn.setRequestMethod("PUT");
                    OutputStreamWriter out = new OutputStreamWriter(
                            conn.getOutputStream());
                    out.write("resource");

                    conn.getInputStream();

                    conn.connect();

                    JSONObject resource = new JSONObject();
                    JSONArray array = new JSONArray();
                    array.put(postDataParams);
                    resource.put("resource", array);

                    postDataParams.put("id_user",id);
                    postDataParams.put("tx_name", nome);
                    postDataParams.put("tx_nickname", nickname);
                    postDataParams.put("password", password);
                    postDataParams.put("nu_cellphone", numcel);
                    postDataParams.put("tx_email", email);
                    Log.e("resource", postDataParams.toString());


                    System.out.println("After this Connection");

                    OutputStream os = conn.getOutputStream();
                    BufferedWriter writer = new BufferedWriter(
                            new OutputStreamWriter(os, "UTF-8"));
                    //writer.write(getPostDataString(postDataParams));
                    writer.write(resource.toString());

                    writer.flush();
                    writer.close();
                    os.close();

                    System.out.println("After this writers");


                    int responseCode = conn.getResponseCode();


                    if (responseCode == HttpsURLConnection.HTTP_OK) {
                        System.out.println("Funcionou!!");
                        Intent intent = new Intent(ActivityAttCad.this, MainActivity2.class);
                        startActivity(intent);


                        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                        StringBuffer sb = new StringBuffer("");
                        String line = "";


                        while ((line = in.readLine()) != null) {

                            sb.append(line);
                            break;
                        }

                        in.close();
                        return sb.toString();

                    } else {
                        return new String("false : " + responseCode);
                    }
                } catch (Exception e) {
                    return new String("Exception: " + e.getMessage());
                }

0 个答案:

没有答案