0

i am trying to insert data in server when i first time RUN program i will insert 2 time but getting error

Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

MyPhp Script

<?php

include("dbConnect.php");

if($_SERVER['REQUEST_METHOD']=='POST')
{


$name = $_REQUEST['name'];
$age = $_REQUEST['age'];
$city = $_REQUEST['city'];
$mob = $_REQUEST['mob'];


 $sql = "INSERT INTO apoint (name,age,city,mob) VALUES('$name','$age','$city','$mob')";


   if(mysqli_query($con,$sql)){

         echo 'successfully registered';
         }else{

         echo 'oops! Please try again!';
         }

         mysqli_close($con);

        }

        else{
        echo 'error'; } ?>

and My Retrofit class

public class RetrofitClient {
    private static Retrofit retrofit = null;

public static Retrofit getClient(String baseUrl) {

    Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    }
    return retrofit;
} }

APIClient

public static final String BASE_URL = "http://192.168.43.88/Appointment/";

public static APIService getAPIService() {

    return RetrofitClient.getClient(BASE_URL).create(APIService.class);
}

Interface

 @POST("insertNew.php")
@FormUrlEncoded

Call<Post> savePost(
        @Field("name") String name,
        @Field("age") String age,
        @Field("city") String city,
        @Field("mob") String mob

);

Post Model

public class Post {


@SerializedName("name")
@Expose
private String name;

@SerializedName("age")
@Expose
private String age;

@SerializedName("city")
@Expose
private String city;

@SerializedName("mob")
@Expose
private String mob;



public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAge() {
    return age;
}

public void setAge(String age) {
    this.age = age;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getMob() {
    return mob;
}

public void setMob(String mob) {
    this.mob = mob;
}


@Override
public String toString() {
    return "Post{" +
            "name='" + name + '\'' +
            ", age='" + age + '\'' +
            ", city='" + city + '\'' +
            ", mob='" + mob + '\'' +
            '}';
}}

Main Fragment Class

mAPIService = ApiUtils.getAPIService();


    btnReg = (Button)linearLayout.findViewById(R.id.btn_apnmnt_reg);

    ((ViewGroup)linearLayout.getParent()).removeView(linearLayout);

    btnReg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String name = etName.getText().toString().trim();
            String age = etAge.getText().toString().trim();
            String city = etCity.getText().toString().trim();
            String mob = etMob.getText().toString().trim();

            sendPost(name,age,city,mob);

            if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(age) && !TextUtils.isEmpty(city) && !TextUtils.isEmpty(mob)){
                sendPost(name,age,city,mob);

            }

        }
    });


    return linearLayout;
}


public void sendPost(String name, String age, String city, String mob) {
    mAPIService.savePost(name, age,city,mob).enqueue(new Callback<Post>() {


        @Override
        public void onResponse(Call<Post> call, Response<Post> response) {

            if(response.isSuccessful()) {

                Log.i(TAG, "post submitted to API." + response.body().toString());
                Toast.makeText(getActivity(),"post submitted to API." +response.body().toString(),Toast.LENGTH_LONG).show();

            }
        }

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

            Toast.makeText(getActivity(),"Unable to submit post to API." + t.toString(),Toast.LENGTH_LONG).show();
            Log.i(TAG, "Unable to submit post to API." +t.toString());

        }
    });
}

Can Anyone Tell Me Why i am getting Error where i doing wrong

Anil
  • 1,605
  • 1
  • 14
  • 24

2 Answers2

1

You defined yor API call for retrofit as

Call<Post> savePost(
        @Field("name") String name,
        @Field("age") String age,
        @Field("city") String city,
        @Field("mob") String mob

);

Which maens your PHP must return JSON object which can be parsed into your Post model. But the script is just returning strings.

You may return proper JSON data object on success (just build it as array from obtained data and serialize with json_encode). For errors you may use corresponding http response codes ( PHP: How to send HTTP response code? ).

However it seem's you are not using the response at all and you can start with changing your interface for savePost. Check this response and work your way to some better solution later: How to get response as String using retrofit without using GSON or any other library in android

Josef Adamcik
  • 5,620
  • 3
  • 36
  • 42
0

Please have a look at the response of your request. Retrofit expects a json response by default. But your php script returns just string (success/error).

You hvae return a json response, example php:

$responseData = array();
// do you your custom logic
$responseData ['success'] = true/false;
...
echo json_encode($responseData);
BenRoob
  • 1,662
  • 5
  • 22
  • 24