Android app not displaying message stored as string in Yii Controller


(Jamess) #1

I have a model named Messages which contains one public variable $data. In the controllers I have as below:

<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\Message;

class MessageController extends Controller
{
    public function actionDisplay()
	{
	  $model= new Message();
		
	  $model->data="Hello";

	  return $model->data;
	}
}

My android code is as below:
package com.example.anitaa.student;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity extends AppCompatActivity {
TextView text;
Button b1;
String url1=“http://192.168.1.3/student/web/index.php?r=message/display”;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(TextView) findViewById(R.id.text_content);
b1=(Button) findViewById(R.id.btn5);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final RequestQueue requestQueue1= Volley.newRequestQueue(MainActivity.this);
StringRequest stringRequest1=new StringRequest(Request.Method.POST, url1, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
text.setText(response);
requestQueue1.stop();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
text.setText(“Error”);
error.printStackTrace();
requestQueue1.stop();
}
});
requestQueue1.add(stringRequest1);
}
});
}
}


(Stefano Mtangoo) #2

Hi!
Please don’t dump your Android Code. Not many here will understand it as this is Yii forum
Few things to help you:

  1. Did you test your API URL with Postman or similar tool? Is it working?
  2. Can you log request and response in your Android app? Retrofit does a good job on that compared to Volley
  3. If (1) and (2) does not give you any clue can you use PHP and Android Debuggers to inspect the changes?

(Jamess) #3

When I try to view the error message on Toast command in android, then the error is com.android.volley.ClientError
What should I do?


(Stefano Mtangoo) #4

Just do what I said earlier!