0

I have a json file with parameters name and hit.

[{
    "name": "pavan",
    "hit": true   
}]

I would like to update name and hit to

[{
    "name": "sai",
    "hit": false
}]

I tried using put but it is not updating the values.

protected Void doInBackground(Void... voids) {

      /*  x = new ArrayList<Entry>();
        y = new ArrayList<String>();*/
        try {
            URL url=new URL("https://api.myjson.com/bins/1854yb");
            HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
            InputStream inputStream=httpURLConnection.getInputStream();
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
            String line="";
            while (line!=null)
            {
                line=bufferedReader.readLine();
                data=data+line;
            }
            JSONArray JA=new JSONArray(data);
            for(int i=0;i<JA.length();i++)
            {
                JSONObject JO= (JSONObject) JA.get(i);
                singleparsed="Name:"+JO.get("name")+"\n"+
                        "Hit:"+JO.get("hit");

                JO.put("name","sai");
                JO.put("hit",false);

                dataparsed=dataparsed+singleparsed;
            }


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

I want to update any element in that file, but it's not working.

deadfish
  • 11,996
  • 12
  • 87
  • 136
pavan sai
  • 19
  • 1
  • 2
  • How do you know it's not working? By looking at `singleparsed`? If so, it's normal, you need to move it after the lines where you put the new values. – Adinia Jun 05 '19 at 08:33
  • Yeah you were right but its not updating in link why?? – pavan sai Jun 05 '19 at 09:12
  • Well, then show us all the code; here you are just creating a copy to the `JO` object with the new values, but you are not using it, nor publishing it back to the url. – Adinia Jun 05 '19 at 09:12
  • this was the code and to post it back what to do?? Thanks – pavan sai Jun 05 '19 at 09:31
  • That's entirely another question; see [Android-POST Json with HttpUrlConnection](https://stackoverflow.com/q/40238360) – Adinia Jun 05 '19 at 09:45
  • i have gone through that but i didnt get where to use that method in above code??please help – pavan sai Jun 05 '19 at 12:24

2 Answers2

1

You can override the value as you want putting the same key.

[ { "name": "pavan", "hit": true } ]

JsonObject jsonObj = new JsonObject();
jsonObj .put("name", "Sai");
jsonObj .put("hit", false); 
  • this is not working kindly see the above code my JSONOBJECT name is JO and i am doing same but its not working?? – pavan sai Jun 05 '19 at 06:35
  • make another object. JsonArray jsonArray = new JsonArray();JsonObject jsonObj = new JsonObject(); jsonObj .put("name", "Sai"); jsonObj .put("hit", false); jsonArray.put(jsonObj); – Chandan Yadav Jun 06 '19 at 06:25
0

We can use JSON Object request instead of String request in Volley.

JSONObject obj = JSONObject()
obj.put("name", "xxx")
obj.put("hit", false)

Log.i("OutPut : ",obj.toString())
Mathan
  • 1
  • 2