Issue
Hi in the below i have list of arrays named as cct and intensity.Iam trying converting all the integers into single arraylist and the am sending the arraylist to the server as a request body.
Now am trying to passing arraylist as a parameter to requestbody it was showing error.
can any one please help me how to pass as a paramets
public void getCCTAndIntensityValues (String schedulerType){
int cct1 = Integer.parseInt(String.valueOf(mCCT1.getProgress()));
int cct2 = Integer.parseInt(String.valueOf(mCCT2.getProgress()));
int cct3 = Integer.parseInt(String.valueOf(mCCT3.getProgress()));
int cct4 = Integer.parseInt(String.valueOf(mCCT4.getProgress()));
int cct5 = Integer.parseInt(String.valueOf(mCCT5.getProgress()));
int cct6 = Integer.parseInt(String.valueOf(mCCT6.getProgress()));
int cct7 = Integer.parseInt(String.valueOf(mCCT7.getProgress()));
int cct8 = Integer.parseInt(String.valueOf(mCCT8.getProgress()));
int cct9 = Integer.parseInt(String.valueOf(mCCT9.getProgress()));
int cct10 = Integer.parseInt(String.valueOf(mCCT10.getProgress()));
int cct11 = Integer.parseInt(String.valueOf(mCCT11.getProgress()));
int cct12 = Integer.parseInt(String.valueOf(mCCT12.getProgress()));
int intensity1 = Integer.parseInt(String.valueOf(mIntenisty1.getProgress()));
int intensity2 = Integer.parseInt(String.valueOf(mIntenisty2.getProgress()));
int intensity3 = Integer.parseInt(String.valueOf(mIntenisty3.getProgress()));
int intensity4 = Integer.parseInt(String.valueOf(mIntenisty4.getProgress()));
int intensity5 = Integer.parseInt(String.valueOf(mIntenisty5.getProgress()));
int intensity6 = Integer.parseInt(String.valueOf(mIntenisty6.getProgress()));
int intensity7 = Integer.parseInt(String.valueOf(mIntenisty7.getProgress()));
int intensity8 = Integer.parseInt(String.valueOf(mIntenisty8.getProgress()));
int intensity9 = Integer.parseInt(String.valueOf(mIntenisty9.getProgress()));
int intensity10 = Integer.parseInt(String.valueOf(mIntenisty10.getProgress()));
int intensity11 = Integer.parseInt(String.valueOf(mIntenisty11.getProgress()));
int intensity12 = Integer.parseInt(String.valueOf(mIntenisty12.getProgress()));
ArrayList<Integer> number =new ArrayList<>();
number.add(intensity1);
number.add(intensity2);
number.add(intensity3);
number.add(intensity4);
number.add(intensity5);
number.add(intensity6);
number.add(intensity7);
number.add(intensity8);
number.add(intensity9);
number.add(intensity10);
number.add(intensity11);
number.add(intensity12);
ArrayList<Integer> cctvalues =new ArrayList<>();
cctvalues.add(cct1);
cctvalues.add(cct2);
cctvalues.add(cct3);
cctvalues.add(cct4);
cctvalues.add(cct5);
cctvalues.add(cct6);
cctvalues.add(cct7);
cctvalues.add(cct8);
cctvalues.add(cct9);
cctvalues.add(cct10);
cctvalues.add(cct11);
cctvalues.add(cct12);
// cct = "[" + cct1 + " " + cct2 + " " + cct3 + " " + cct4 + " " + cct5 + " " + cct6 + " " + cct7 + " " + cct8 + " " + cct9 + " " + cct10 + " " + cct11 + " " + cct12 + "]";
// intensity = "[" + intensity1 + " " + intensity2 + " " + intensity3 + " " + intensity4 + " " + intensity5 + " " + intensity6 + " " + intensity7 + " " + intensity8 + " " + intensity9 + " " + intensity10 + " " + intensity11 + " " + intensity12 +"]";
RequestAsyntask mRequestAsyntask = new RequestAsyntask(mContext);
try {
RequestBody requestBody = new FormEncodingBuilder().add("light_id", lightid).add("intensity", cctvalues.add(cct1)).add("cct",cctvalues ).build();
Request request = new Request.Builder().url(BASE_URL + schedulerType).post(requestBody).build();
mRequestAsyntask.runDataRetriver(request, false);
Log.i(TAG, "Sending---" + BASE_URL + schedulerType + "\n" + "light_id:" + lightid + "\n" + "intensity:" + intensity + "\n" + "cct:" + cct);
mRequestAsyntask.setResponseListener(new ResponseCallback() {
@Override
public void success(String response) {
Log.i(TAG, "Response:" + response);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
Solution
You need to change your request for API calling, You need to convert your ArrayList
into the JSONArray
like below:-
try {
ArrayList<Integer> number =new ArrayList<>();
number.add(intensity1);
number.add(intensity2);
number.add(intensity3);
number.add(intensity4);
number.add(intensity5);
number.add(intensity6);
number.add(intensity7);
number.add(intensity8);
number.add(intensity9);
number.add(intensity10);
number.add(intensity11);
number.add(intensity12);
JSONArray numberJson = new JSONArray();
for (int i = 0; i < number.size(); i++) {
numberJson.put(i, number.get(i));
}
ArrayList<Integer> cctvalues = new ArrayList<>();
cctvalues.add(cct1);
cctvalues.add(cct2);
cctvalues.add(cct3);
cctvalues.add(cct4);
cctvalues.add(cct5);
cctvalues.add(cct6);
cctvalues.add(cct7);
cctvalues.add(cct8);
cctvalues.add(cct9);
cctvalues.add(cct10);
cctvalues.add(cct11);
cctvalues.add(cct12);
JSONArray cctJsonArray = new JSONArray();
for (int i = 0; i < cctvalues.size(); i++) {
cctJsonArray.put(i, cctvalues.get(i));
}
RequestBody requestBody = new FormEncodingBuilder()
.add("light_id", lightid)
.add("intensity", cctJsonArray.toString()).
.add("cct",numberJson.toString() ).build();
/**
* DO YOUR STUFF
*/
}
catch (Exception ex)
{
ex.printStackTrace();
}
Answered By - Ravindra Kushwaha
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.