0

I have a Wordpress Form and i want to send some data to it by retrofit but the problem is ... in API .. i have the fields name but dont have it as a JSONOBJECt so.. how can i send data to it the API is

[
  {
    "id": 1368,
    "date": "2014-04-29T02:02:39",
    "date_gmt": "2014-04-28T23:02:39",
    "guid": {
      "rendered": ""
    },
    "modified": "2014-04-30T14:52:32",
    "modified_gmt": "2014-04-30T11:52:32",
    "slug": "%d8%a7%d8%b1%d8%b3%d9%84-%d8%a7%d8%b3%d8%aa%d8%b4%d8%a7%d8%b1%d8%aa%d9%83",
    "status": "publish",
    "type": "page",
    "link": "",
    "title": {
      "rendered": "ارسل استشارتك"
    },
    "content": {
      "rendered": "<p>[vc_row][vc_column width=&#8221;1/1&#8243;]<div role=\"form\" class=\"wpcf7\" id=\"wpcf7-f1381-o1\" lang=\"ar\" dir=\"rtl\">\n<div class=\"screen-reader-response\"></div>\n<form action=\"/wp-json/wp/v2/pages#wpcf7-f1381-o1\" method=\"post\" class=\"wpcf7-form\" novalidate=\"novalidate\">\n<div style=\"display: none;\">\n<input type=\"hidden\" name=\"_wpcf7\" value=\"1381\" />\n<input type=\"hidden\" name=\"_wpcf7_version\" value=\"5.0.3\" />\n<input type=\"hidden\" name=\"_wpcf7_locale\" value=\"ar\" />\n<input type=\"hidden\" name=\"_wpcf7_unit_tag\" value=\"wpcf7-f1381-o1\" />\n<input type=\"hidden\" name=\"_wpcf7_container_post\" value=\"0\" />\n</div>\n<p><strong>هل لديك أي أسئلة تخص صحتك النفسية؟ لا تتردد وقم بمراسلتنا الآن - جميع البيانات الشخصية تخضع لسياسة الخصوصية</strong></p>\n<p>أسمك الكريم (مطلوب)<br />\n    <span class=\"wpcf7-form-control-wrap your-name\"><input type=\"text\" name=\"your-name\" value=\"\" size=\"40\" class=\"wpcf7-form-control wpcf7-text wpcf7-validates-as-required\" aria-required=\"true\" aria-invalid=\"false\" /></span> </p>\n<p>بريدك الإلكتروني (مطلوب)<br />\n    <span class=\"wpcf7-form-control-wrap your-email\"><input type=\"email\" name=\"your-email\" value=\"\" size=\"40\" class=\"wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email\" aria-required=\"true\" aria-invalid=\"false\" /></span> </p>\n<p>العنوان<br />\n    <span class=\"wpcf7-form-control-wrap your-subject\"><input type=\"text\" name=\"your-subject\" value=\"\" size=\"40\" class=\"wpcf7-form-control wpcf7-text\" aria-invalid=\"false\" /></span> </p>\n<p>سجلك المرضى<br /><span class=\"wpcf7-form-control-wrap text-583\"><input type=\"text\" name=\"text-583\" value=\"\" size=\"40\" class=\"wpcf7-form-control wpcf7-text\" aria-invalid=\"false\" /></span> </p>\n<p>عقاقير تتناولها<br /><span class=\"wpcf7-form-control-wrap text-623\"><input type=\"text\" name=\"text-623\" value=\"\" size=\"40\" class=\"wpcf7-form-control wpcf7-text\" aria-invalid=\"false\" /></span> </p>\n<p>مدة تناول العقاقير<br /><span class=\"wpcf7-form-control-wrap text-767\"><input type=\"text\" name=\"text-767\" value=\"\" size=\"40\" class=\"wpcf7-form-control wpcf7-text\" aria-invalid=\"false\" /></span></p>\n<p>رسالتك<br /><span class=\"wpcf7-form-control-wrap your-message\"><textarea name=\"your-message\" cols=\"40\" rows=\"10\" class=\"wpcf7-form-control wpcf7-textarea\" aria-invalid=\"false\"></textarea></span> </p>\n<p><input type=\"submit\" value=\"إرسال\" class=\"wpcf7-form-control wpcf7-submit\" /></p>\n<div class=\"wpcf7-response-output wpcf7-display-none\"></div></form></div>[/vc_column][/vc_row]</p>\n",
      "protected": false
    },
    "excerpt": {
      "rendered": "<p>[vc_row][vc_column width=&#8221;1/1&#8243;][/vc_column][/vc_row]</p>\n",
      "protected": false
    },
    "author": 1,
    "featured_media": 0,
    "parent": 0,
    "menu_order": 0,
    "comment_status": "open",
    "ping_status": "open",
    "template": "page-homepage-blank.php",
    "meta": [],
    "_links": {
      "self": [
        {
          "href": ""
        }
      ]
    }
  }
]

and the fields are in content JSONARRAY

and this is what in my interface

@FormUrlEncoded
    @POST("pages/1368")
    Call<Person> askUs(@Field("your-name") String Name ,
                       @Field("your-email") String Email ,
                       @Field("your-message") String Message);

and in my api class

public void sendMessage(Callback<Person> callback , String name , String email , String message){
        Call<Person> person = service.askUs(name , email , message);
        person.enqueue(callback);
    }

and in my Activity

MyApp.apiManager.sendMessage(new Callback<Person>() {
                @Override
                public void onResponse(Call<Person> call, Response<Person> response) {

                    if (response.isSuccessful())
                    {
                        Toast.makeText(getContext(), "succeed", Toast.LENGTH_SHORT).show();

                        Log.i("MessageIs",response.body().toString());
                    } else
                    {

                        Toast.makeText(getContext(), "Failed" + response.toString() + response.message(), Toast.LENGTH_SHORT).show();
                        Log.i("ErrorIs","Failed" + response.toString() + response.message());
                    }
                }

                @Override
                public void onFailure(Call<Person> call, Throwable t) {

                    Toast.makeText(getContext(), "Error", Toast.LENGTH_SHORT).show();

                    Log.i("MessageIs",t.getLocalizedMessage());

                }
            } ,Name , Email, Message);

and its giving me code=401, message=Authorization Required

2 Answers2

0

First of all you should have Exact model class representing your JSON , assumed the object of same Model is filled with above contents you can use the following approch

In Retrofit call use @Field("FieldNAME") int yourVariableName,

Abhinandan
  • 128
  • 8
  • i used www.jsonschema2pojo.org and it gave me the main class in it list of content and in content class it was rendered so where i should put it – Muhammad Nashat Aug 11 '18 at 08:23
0

Yo should use @Field("FieldName) as well as annotate it with @FormUrlEncoded this necessary to avoid problems.

sample would be like this:

    @FormUrlEncoded
    @POST(Urls.CHNAGE_PASSWORD_URL)
    Call<ChangePasswordResponse> changePassword(@Field("user_id") String 
    userId);
Ali Habbash
  • 777
  • 2
  • 6
  • 29
  • thank u .. but why its giving me Authorization Required 401 error ? – Muhammad Nashat Aug 12 '18 at 10:33
  • do you have a header Authorization? if yes you need to add a `@Header("parameter-name")` parameter to the request parameters or refer to this question https://stackoverflow.com/questions/32605711/adding-header-to-all-request-with-retrofit-2 – Ali Habbash Aug 12 '18 at 12:36
  • i dont think that its needing a header authorization its just a normal form anyone can send a message from it to the admin – Muhammad Nashat Aug 12 '18 at 13:15
  • refer to this question https://stackoverflow.com/questions/42375566/getting-401-unauthorised-in-retrofit-but-works-in-postman-and-swift/43277229 – Ali Habbash Aug 13 '18 at 17:00