I have google it this issue but i'm not able to find the solution. I think guys you can help me to sort out the issue.
I am using retrofit library first time .
My issue is while i am posting the request through retrofit. i am getting below error
D/onFailure: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
**main activity **
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.Date;
import java.util.List;
import retrofit.Call;
import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;
public class MainActivity extends AppCompatActivity {
String url = "http://www.example.com/App/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getRetrofitObject();
}
void getRetrofitObject() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitObjectAPI service = retrofit.create(RetrofitObjectAPI.class);
Call<List<district>> call = service.getDistrictDetails("mdistrict","search");
call.enqueue(new Callback<List<district>>() {
@Override
public void onResponse(Response<List<district>> response, Retrofit retrofit) {
try {
//Log.e("response---",response.toString());
List<district> StudentData = response.body();
// Log.e("StudentData---",StudentData.toString());
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable t) {
Log.d("onFailure", t.toString());
}
});
}
}
RetrofitObjectAPI
import java.util.List;
import retrofit.Call;
import retrofit.http.Field;
import retrofit.http.FormUrlEncoded;
import retrofit.http.GET;
import retrofit.http.POST;
import retrofit.http.Query;
public interface RetrofitObjectAPI {
@FormUrlEncoded
@POST("/kwa/api/")
// Call<List<district>> getDistrictDetails( );
Call<List<district>> getDistrictDetails(@Field("target") String target, @Field("action") String action);
}
Gradle
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.google.code.gson:gson:1.7.2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.okhttp:okhttp:2.4.0'
my php code
<?php
$_errAry = array("status"=>400, "success"=>"false", "message"=>"Can't Service your request ","data"=>array());
$_sucAry = array("status"=>200, "success"=>"true", "message"=>"","data"=>array());
//echo json_encode($_POST);
include_once 'database.php';
// get database connection
$database = new Database();
$db = $database->getConnection();
if(!isset($_POST["target"],$_POST["action"])){echo json_encode($_errAry);exit(0);}
// instantiate Common Objects
include_once 'objects/common.class.php';
include_once 'objects/validate.php';
$_tbl=""; $_cols=""; $_ord=""; //$_whr=" AND sr_status ='0' ";
$_whr=" ";
$_max_date = (isset($_POST["max_date"])) ? $_POST["max_date"] : "";
$_max_id = (isset($_POST["max_id"])) ? (int)$_POST["max_id"] : "";
$_imei = (isset($_POST["imei"])) ? $_POST["imei"]: "";
if($_POST["target"]=="mdistrict" && $_POST["action"]=="search"){
$_comm = new Common($db);
$_stmt = $_comm->readAll("master_district", "sr_id,sr_name", $_whr , " 1 asc ");
$_data = array();
while ($_row = $_stmt->fetch(PDO::FETCH_ASSOC)){
$_tmpAry = array(
"sr_id"=> $_row["sr_id"],
"sr_name"=> trim($_row["sr_name"])
);
$_data[] = $_tmpAry;
}
$_op = $_sucAry;
$_op["data"]=$_data;
echo json_encode($_op);
exit(0);
}
$_max_date = (isset($_POST["max_date"])) ? $_POST["max_date"] : "";
$_max_id = (isset($_POST["max_id"])) ? (int)$_POST["max_id"] : "";
$_imei = (isset($_POST["imei"])) ? $_POST["imei"]: "";
?>
Can anyone please help me to solve this issue. Please find where i done the mistakes
MY result json
{"status":200,"success":"true","message":"","data":[{"sr_id":"1","sr_name":"ALAPPUZHA"},{"sr_id":"2","sr_name":"KOTTAYAM"}]}