我有一个问题,我尝试在将值传递给PHP文件后如何获取消息,然后如何显示massge的值 从我尝试过的php文件中,但是我收到一条错误消息,如何从php文件中获取一条消息,以便它是php文件中写入的Non或Don消息的结果,请参见我的代码
// Log.i: msg: error loading from API java.lang.IllegalStateException:Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
// MainActivity class:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// updateList();
post_1();
}
}, 300);
}
public void post_1(){
ApiUtil.getServiceClass().getAllPost("ahmed").enqueue(new Callback<List<ApiObject>>() {
@Override
public void onResponse(Call<List<ApiObject>> call,Response<List<ApiObject>>response) {
if(response.isSuccessful()){
List<ApiObject> postList = response.body();
Log.i("msg", "Returned msg " + response.message().toString());
//Log.i("msg", "Returned count " + postList.size());
}
}
@Override
public void onFailure(Call<List<ApiObject>> call, Throwable t) {
//showErrorMessage();
Log.i("msg", "error loading from API "+t.toString());
}
});
}
// End ;
}
// interface :
public interface RetrofitInterface {
@GET("get.php")
public Call<List<ApiObject>> getAllPost(@Query("name") String user_name);
}
// ApiUtil class :
public class ApiUtil {
private static final String BASE_URL = "http://192.168.1.3/test/";
public static RetrofitInterface getServiceClass(){
return RetrofitAPI.getRetrofit(BASE_URL).create(RetrofitInterface.class);
}
}
// ApiObject class :
public class ApiObject {
@SerializedName("name")
private String name;
@SerializedName("message")
private String message;
public ApiObject(String user_name, String message) {
this.name = user_name;
this.message = message;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
// php file :
<?php
// include Marei DB Class
include 'DB.php';
// get content input and create json object to parse it
$data = file_get_contents("php://input");
$obj = json_decode($data);
// create db instance to use marei db queris
$db = DB::getInstance();
// set type of header response to application/json for respone
header('Content-Type: application/json');
if(!$_GET["name"]) {
print json_encode(['status' => 0, 'message' => 'Username is Non !']);
}else{
print json_encode(['status' => 1, 'message' => 'Username is Don !']);
}
?>