Chrome中的函数返回有效的JSON,而PostMan中的函数也如下所示:
{"name":"Not Found","message":"Page not found.","code":0,"status":404,"type":"yii\\web\\NotFoundHttpException","previous":{"name":"Invalid Route","message":"Unable to resolve the request: api/v1/visitor/x","code":0,"type":"yii\\base\\InvalidRouteException"}}
Java代码:
final JSONObject json = new JSONObject();
final TextView visitorMobile = findViewById(R.id.visitor_mobile);
final String url = getResources().getString(R.string.url_search_mobile);
final String urlP = url+mobileInput;
/*try {
//json.put("mobile", visitorMobile.getText());
json.put("Content-Type","application/json");
} catch (JSONException e) {
e.printStackTrace();
}*/
Log.d(TAG, "numbersList: "+ urlP);
visitorMobile.onEditorAction(EditorInfo.IME_ACTION_DONE); // hide the keyboard
final ProgressDialog dialog = ProgressDialog.show(this, null, "Please Wait"); //loading dialog
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, urlP, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
dialog.dismiss();
try {
int status = response.getInt("status");
String message = response.getString("message");
if(status == 400) {
WebServiceUtils.showDialog(VisitorForm.this, "Error" ,message);
}else if(status == 201){
WebServiceUtils.showDialog(VisitorForm.this, "Success" ,message);
//Intent SendOtpActivity = new Intent(SendMobileActivity.this, SendOtpActivity.class);
//startActivity(SendOtpActivity);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
PHP代码:
public function actionMobile ($mobile) {
$search = Visitor::findByMobile($mobile);
throw new \yii\base\Exception() ; //for testing only
if($search){
return ["result"=>$search];
return \Yii::$app->response->data = ['status' => '200', 'result' => $search];
}
\Yii::$app->response->data = ['status' => '401', 'message' => 'Not Found!'];
}
Android Studio中的错误 E/Volley: [1413] BasicNetwork.performRequest: Unexpected response code 500 for http://localhost/api/v1/visitor/mobile/165
截击错误响应的结果:
D/VisitorForm: ERRORS
ERROR: com.android.volley.ServerError
MESSAGE: null
CAUSE: null
D/VisitorForm: HEADERS: [Header[name=Connection,value=close], Header[name=Content-Type,value=application/json; charset=UTF-8], Header[name=Date,value=Thu, 06 Sep 2018 20:45:36 GMT], Header[name=Server,value=Apache/2.4.33 (Unix) PHP/7.2.8], Header[name=Transfer-Encoding,value=chunked], Header[name=X-Android-Received-Millis,value=1536266735555], Header[name=X-Android-Response-Source,value=NETWORK 500], Header[name=X-Android-Selected-Protocol,value=http/1.1], Header[name=X-Android-Sent-Millis,value=1536266735521], Header[name=X-Powered-By,value=PHP/7.2.8]]
DATA: [B@b208d24
STATUS: 500
我希望有人对此提供帮助,因为我无法在应用中显示错误消息。