0
votes

I did not find the answer to many search results. The program cannot read the data when I use the variable, without the variable I can see the whole database.

  1. I use the button(idBtnGetCourse) and EditText(idEdtCourseName).
  2. I use the PHP code.

I also have: CourseAdapter.Java, course_rv_item.xml.

I use in part: link

mainactivity code:

public class MainActivity extends AppCompatActivity {

private RecyclerView courseRV;
private EditText idEdtCourseName;
private Button idBtnGetCourse;
private CourseAdapter adapter;
private ArrayList<CourseModal> courseModalArrayList;
String url = "http://......../vlookup.php";
private ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    idEdtCourseName = findViewById(R.id.idEdtCourseName);
    idBtnGetCourse = findViewById(R.id.idBtnGetCourse);
    courseRV = findViewById(R.id.idRVCourses);
    progressBar = findViewById(R.id.idPB);

    courseModalArrayList = new ArrayList<>();

    buildRecyclerView();

    idBtnGetCourse.setOnClickListener(v -> {
         getData(idEdtCourseName.getText().toString());
    });

}

private void getData(String s) {
   
    RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, 
null, response -> {
        progressBar.setVisibility(View.GONE);
        courseRV.setVisibility(View.VISIBLE);

        for (int i = 0; i < response.length(); i++) {

            try {
                JSONObject responseObj = response.getJSONObject(i);

                String courseName = responseObj.getString("title");
                String courseTracks = responseObj.getString("name");
                String courseMode = responseObj.getString("score");
                String courseImageURL = responseObj.getString("scoreb");
                courseModalArrayList.add(new CourseModal(courseName, courseImageURL, 
 courseMode, courseTracks));
                buildRecyclerView();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },  error -> {
        
        Toast.makeText(MainActivity.this, "Fail to get course" + error, 
Toast.LENGTH_SHORT).show();
    })
    {
        @Override
        public String getBodyContentType() {

            return "application/x-www-form-urlencoded; charset=UTF-8";
        }

        @Override
        protected Map<String, String> getParams() {

            Map<String, String> params = new HashMap<>();

            params.put("აბონენტი", s);

            return params;
        }
    };
    queue.add(jsonArrayRequest);
  }

  private void buildRecyclerView() {


    adapter = new CourseAdapter(courseModalArrayList, MainActivity.this);

    LinearLayoutManager manager = new LinearLayoutManager(this);
    courseRV.setHasFixedSize(true);

    courseRV.setLayoutManager(manager);

    courseRV.setAdapter(adapter);
  }
  }

vlookup.PHP code: not work (org.json.JSONObject cannot be converted to JSONArray in android)

 <?php
 $servername = "localhost";
 $username = "root";
 $password = "";
 $dbname = "gfgdatabase";


 $conn = new mysqli($servername, $username, $password, $dbname);
 $response = array();
 $posts = array();

 if(isset($_POST['აბონენტი'])){
 $აბონენტი = $_POST['აბონენტი'];

 $sql = "SELECT * from substand 
    RIGHT JOIN coursedb on substand.მრიცხველი=coursedb.მრიცხველი
    WHERE coursedb.აბონენტი = '$აბონენტი'";

 $result=mysqli_query($conn,$sql);



 while($row=mysqli_fetch_array($result)) { 
  $title=$row['მრიცხველი']; 
  $name=$row['ჯგუფი']; 
  $score=$row['სახელი_გვარი'];
  $scoreb=$row['მისამართი'];

  $response[] = array('title'=> $title, 'name'=> $name, 'score'=> $score, 'scoreb'=> 
$scoreb);
} 
} else{

    $response['error'] = true;
    $response['message'] = "Insufficient Parameters";
 }

 echo json_encode($response);

?> 

vlookupcopy.PHP code(worker, without EditText):

 <?php
 $servername = "localhost";
 $username = "secondd";
 $password = "Toko539397";
 $dbname = "id16310745_gfgdatabase";


 $conn = new mysqli($servername, $username, $password, $dbname);
 $response = array();
 $posts = array();

 $sql = "SELECT * from substand 
    RIGHT JOIN coursedb on substand.მრიცხველი=coursedb.მრიცხველი
    WHERE coursedb.აბონენტი = 353535";

 $result=mysqli_query($conn,$sql);


 while($row=mysqli_fetch_array($result)) { 
   $title=$row['მრიცხველი']; 
   $name=$row['ჯგუფი']; 
   $score=$row['სახელი_გვარი'];
   $scoreb=$row['მისამართი'];

    $response[] = array('title'=> $title, 'name'=> $name, 'score'=> $score, 'scoreb'=> 
 $scoreb);
 } 

echo json_encode($response);

 ?> 

I have checked both codes in Postman, I get good results on both PHP codes.

Both codes contain an array. Why is it that one works and the other where the variable does not?

What should I change in the code? Thanks everyone, for any advice.

Your response will be a JSON Array if the query works (an array of objects), a JSON Object if it returns the error, because of the way the json will be encoded.aynber
More accurately if you can? I know that have to correct: JSON Array and JSON Object, But I do not know how :(tornike kharitonishvili