Как использовать залп для публикации объекта json в Watson Studio API - PullRequest
1 голос
/ 27 июня 2019

Я хочу отправить этот тип данных в мой развернутый API модели машинного обучения в IBM Watson-Studio из приложения для Android, использующего Volley.

{
   "fields":[
      "Gender",
      "Age",
      "EstimatedSalary"
   ],
   "values":[
      Gender,
      Age,
      EstimatedSalary
   ]
}

Я застрял при создании JSON-объекта для этого требования и отправил его в API.

Я застрял при создании JSON-объекта и не могу продолжить

protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("fields","Gender");
                params.put("values",Gender);
                return super.getParams();
            }

Я знаю, что это не работает. Но не понимаю, как поступить.

1 Ответ

0 голосов
/ 27 июня 2019

У меня есть ответ после построения JSONArray.Если у кого-то есть тот же вопрос, вот ответ.

JSONObject  jsonObject=new JSONObject();
        try {
            JSONArray jsonArrayFields=new JSONArray();
                jsonArrayFields.put("Age");
                jsonArrayFields.put("Gender");
                jsonArrayFields.put("EstimatedSalary");

            jsonObject.put("results",jsonArrayFields);

            JSONArray jsonArrayValues = new JSONArray();
                jsonArrayValues.put(Age);
                jsonArrayValues.put(Gender);
                jsonArrayValues.put(EstimatedSalary);

            JSONArray jsonArrayValues2D = new JSONArray();
                jsonArrayValues2D.put(jsonArrayValues); //surrounding the jsonArrayValues in another Array

            jsonObject.put("values",jsonArrayValues2D);

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

//and finally to send to the API
 JsonObjectRequest request_json = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
           new Response.Listener<JSONObject>() {
               @Override
               public void onResponse(JSONObject response) {
                   try {
                       //on successful response
                   } catch (JSONException e) {
                       e.printStackTrace();
                   }
               }
           }, new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
                   VolleyLog.e("Error: ", error.getMessage());
               }
           });

    // add the request object to the queue to be executed
    RequestQueue queue = Volley.newRequestQueue(this);
    queue.add(request_json);
...