gpt4 book ai didi

android - 如何在android中的截击中发送参数数组

转载 作者:行者123 更新时间:2023-12-05 05:13:17 27 4
gpt4 key购买 nike

我想使用 volley API 将数据传递给服务器。参数如下:

Request:
{
"user_id" : 6,
"package_name": "Personal Package",
"care_type" : ["help in shopping", "Personal care"],
"care_delivered" : "as day care",
"gender" : "F",
"experience" : "4,0",
"skills":"very helpful , caring, etc",
"start_date" : "2018-09-12",
"price": "200-700, day",
"description": "test"
}

在这个care_type中是一个数组类型。我已经完成了发送数据的代码,但每次我都收到错误,即

onErrorResponse: Error: org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

我的 API 代码是:

 private void createPackageApi() {
String tag_json_obj = "json_obj_req";
String url = Constants.CREATE_PACKAGE;


pBar.setVisibility(View.VISIBLE);
Activity activity = getActivity();
if (activity != null && isAdded()) {
sessionManager = new SessionManager(activity);
}


final HashMap<String, String> packageDetail = sessionManager.getPackageDetail();
JSONArray jsonArray;

try {
jsonArray = new JSONArray(required_support_need);
strArr = new String[jsonArray.length()];

Log.e("tag", "package" + required_support_need + " " + strArr);
for (int i = 0; i < jsonArray.length(); i++) {
strArr[i] = "\"" + jsonArray.getString(i) + "\"";
Log.e("TAG", "getInitializedID:String " + strArr[i]);
}


} catch (Exception e) {

e.printStackTrace();
}


final HashMap<String, String> registrationDetail = sessionManager.getSeekerRegistrationDetail();
HashMap<String, String> params = new HashMap<String, String>();


params.put("user_id", "2");
params.put("package_name", edtPackageNames.getText().toString());
params.put("care_type", Arrays.toString(strArr));
params.put("care_delivered", edtCarePackages.getText().toString());
params.put("gender", edtPreferredGender.getText().toString());
params.put("experience", edtPackageNames.getText().toString());
params.put("skills", edtEssentialSkills.getText().toString());
params.put("start_date", packageDetail.get("start_date"));
params.put("price", edtPackageValue.getText().toString());
params.put("description", edtPackageDescription.getText().toString());


Log.e("TAG", "seekerPackage: " + params);


JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, new JSONObject(params),
new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {

Log.e("package_detail_response", response.toString());

Gson gson = new Gson();
try {
if (response.has("success")) {

CreatePackage createPackage;
createPackage = gson.fromJson(response.toString(), CreatePackage.class);

String status = createPackage.getSuccess();

if (status.equals("success")) {
Activity activity = getActivity();
if (activity != null && isAdded()) {
Toast.makeText(getActivity(), R.string.package_created, Toast.LENGTH_LONG).show();
}
}

pBar.setVisibility(View.GONE);

} else {


ResponseError responseError;
responseError = gson.fromJson(response.toString(), ResponseError.class);

String msg = responseError.getMessage();
Log.e("TAG", "msg " + msg);
Activity activity = getActivity();
if (activity != null && isAdded()) {
Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
}
}


} catch (Exception e) {

e.printStackTrace();

}

pBar.setVisibility(View.GONE);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pBar.setVisibility(View.GONE);
VolleyLog.e("Error: " + error.getMessage());
// handle error
}
}) {

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=UTF-8");
headers.put("Authorization", "Bearer " + registrationDetail.get("api_token"));
return headers;
}
};


jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
100000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));


AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}

我尝试在参数中发送数组,但没有成功。请告诉我如何在 volley 库中以单个参数发送数组。

最佳答案

要发布数组使用下面的代码

JSONArray care_type = new JSONArray();
for(int i=0; i < yourarray.length(); i++) {
care_type.put(yourarray[i]); // create array and add items into that
}
params.put("care_type",care_type.toString());

关于android - 如何在android中的截击中发送参数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53741524/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com