英文:
Creating table in android studio using recyclerview and ListItem
问题
I understand that you're facing an issue with your Android app where you're trying to display data from an API in a RecyclerView, but the data isn't showing up, and you're encountering a JSON parsing error. The error message you provided indicates that the response from your PHP script is not being parsed correctly as JSON. The issue seems to be with the PHP script itself, as it is echoing HTML tags before the JSON response.
To resolve this issue, you should modify your PHP script to only echo the JSON response without any HTML or other content. Here's the modified PHP script:
<?php
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$send = curl("http://hrd.tvip.co.id/rest_server/api/absensi/index?shift_day=2020-08-24&shift_day_2=2020-08-27&badgenumber=0100028400");
// Output the JSON data directly without any other content
echo $send;
?>
By making this change, your PHP script will only output the JSON response from the API, and your Android app should be able to parse it correctly and display the data in your RecyclerView.
Remember to also check if your API request URL is correct and if the API is returning the expected JSON data. If the URL or API response is incorrect, it could also lead to the issue you're facing.
英文:
I want to create a table in android using ListItem and RecylerView. I already create it using API but it doesn't showing any data. here is my code
kehadiran.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".kehadiran">
<RelativeLayout
android:id="@+id/llViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<TextView
android:id="@+id/kehadiran"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32dp"
android:textColor="@color/colorPrimaryDark"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:layout_centerInParent="true"
android:text = "Kehadiran"
/>
</RelativeLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/llViewTitle"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewDeliveryProductList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
tools:listitem="@layout/table_list_item" />
</HorizontalScrollView>
</RelativeLayout>
table_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/txttglabsen"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@drawable/table_content_cell_bg"
android:textSize="18sp" />
<TextView
android:id="@+id/txtf1"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="@drawable/table_content_cell_bg"
android:textSize="18sp" />
<TextView
android:id="@+id/txtdepoawal"
android:layout_width="124dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@drawable/table_content_cell_bg"
android:textSize="18sp" />
<TextView
android:id="@+id/txtshiftmasuk"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@drawable/table_content_cell_bg"
android:textSize="18sp" />
<TextView
android:id="@+id/txtF4"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@drawable/table_content_cell_bg"
android:textSize="18sp" />
<TextView
android:id="@+id/txtDepoAkhir"
android:layout_width="128dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@drawable/table_content_cell_bg"
android:textSize="18sp" />
<TextView
android:id="@+id/txtKeluar"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@drawable/table_content_cell_bg"
android:textSize="18sp" />
<TextView
android:id="@+id/txtketerangan"
android:layout_width="128dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@drawable/table_content_cell_bg"
android:textSize="18sp" />
</LinearLayout>
TableViewAdapter.java
package com.example.eis2.Item;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.eis2.R;
import java.util.List;
public class TableViewAdapter extends RecyclerView.Adapter {
private List kehadiranlist;
public TableViewAdapter(List kehadiranlist, Context applicationContext) {
this.kehadiranlist = kehadiranlist;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.table_list_item, parent, false);
return new RowViewHolder(itemView);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
RowViewHolder rowViewHolder = (RowViewHolder) holder;
int rowPos = rowViewHolder.getAdapterPosition();
if (rowPos == 0) {
rowViewHolder.txttglabsen.setBackgroundResource(R.drawable.table_header_cell_bg);
rowViewHolder.txtf1.setBackgroundResource(R.drawable.table_header_cell_bg);
rowViewHolder.txtdepoawal.setBackgroundResource(R.drawable.table_header_cell_bg);
rowViewHolder.txtshiftmasuk.setBackgroundResource(R.drawable.table_header_cell_bg);
rowViewHolder.txtF4.setBackgroundResource(R.drawable.table_header_cell_bg);
rowViewHolder.txtDepoAkhir.setBackgroundResource(R.drawable.table_header_cell_bg);
rowViewHolder.txtKeluar.setBackgroundResource(R.drawable.table_header_cell_bg);
rowViewHolder.txtketerangan.setBackgroundResource(R.drawable.table_header_cell_bg);
rowViewHolder.txttglabsen.setText("Tanggal Absen");
rowViewHolder.txtf1.setText("F1");
rowViewHolder.txtdepoawal.setText("Depo Awal");
rowViewHolder.txtshiftmasuk.setText("Shift Masuk");
rowViewHolder.txtF4.setText("F4");
rowViewHolder.txtDepoAkhir.setText("Depo Akhir");
rowViewHolder.txtKeluar.setText("Shift Keluar");
rowViewHolder.txtketerangan.setText("keterangan");
} else {
keteranganmodel modal = (keteranganmodel) kehadiranlist.get(rowPos - 1);
rowViewHolder.txttglabsen.setBackgroundResource(R.drawable.table_content_cell_bg);
rowViewHolder.txtf1.setBackgroundResource(R.drawable.table_content_cell_bg);
rowViewHolder.txtdepoawal.setBackgroundResource(R.drawable.table_content_cell_bg);
rowViewHolder.txtshiftmasuk.setBackgroundResource(R.drawable.table_content_cell_bg);
rowViewHolder.txtF4.setBackgroundResource(R.drawable.table_content_cell_bg);
rowViewHolder.txtDepoAkhir.setBackgroundResource(R.drawable.table_content_cell_bg);
rowViewHolder.txtDepoAkhir.setBackgroundResource(R.drawable.table_content_cell_bg);
rowViewHolder.txtketerangan.setBackgroundResource(R.drawable.table_content_cell_bg);
rowViewHolder.txttglabsen.setText(modal.getShift_day() + "");
rowViewHolder.txtf1.setText(modal.getF1());
rowViewHolder.txtdepoawal.setText(modal.getDepo_f1() + "");
rowViewHolder.txtshiftmasuk.setText(modal.getWaktu_masuk() + "");
rowViewHolder.txtF4.setText(modal.getF4() + "");
rowViewHolder.txtDepoAkhir.setText(modal.getDepo_f4());
rowViewHolder.txtKeluar.setText(modal.getWaktu_keluar() + "");
rowViewHolder.txtketerangan.setText(modal.getKet_absensi() + "");
}
}
@Override
public int getItemCount() {
return kehadiranlist == null ? 0 : kehadiranlist.size();
}
public static class RowViewHolder extends RecyclerView.ViewHolder {
TextView txttglabsen;
TextView txtf1;
TextView txtdepoawal;
TextView txtshiftmasuk;
TextView txtF4;
TextView txtDepoAkhir;
TextView txtKeluar;
TextView txtketerangan;
RowViewHolder(View itemView) {
super(itemView);
txttglabsen = itemView.findViewById(R.id.txttglabsen);
txtf1 = itemView.findViewById(R.id.txtf1);
txtdepoawal = itemView.findViewById(R.id.txtdepoawal);
txtshiftmasuk = itemView.findViewById(R.id.txtshiftmasuk);
txtF4 = itemView.findViewById(R.id.txtF4);
txtDepoAkhir = itemView.findViewById(R.id.txtDepoAkhir);
txtKeluar = itemView.findViewById(R.id.txtKeluar);
txtketerangan = itemView.findViewById(R.id.txtketerangan);
}
}
}
kehadiran.java
package com.example.eis2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
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;
import com.example.eis2.Item.TableViewAdapter;
import com.example.eis2.Item.keteranganmodel;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import static com.example.eis2.Item.LoginItem.ABSENSI_URL;
public class kehadiran extends AppCompatActivity {
private List<keteranganmodel> absensi;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kehadiran);
RecyclerView recyclerView = findViewById(R.id.recyclerViewDeliveryProductList);
TableViewAdapter adapter = new TableViewAdapter(getKehadiranlist(), getApplicationContext());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter);
absensi = new ArrayList<>();
}
private List getKehadiranlist() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, ABSENSI_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
JSONArray keteranganArray = obj.getJSONArray("data");
for (int i = 0; i < keteranganArray.length(); i++) {
JSONObject keteranganobject = keteranganArray.getJSONObject(i);
keteranganmodel keterangan = new keteranganmodel(
keteranganobject.getString("shift_day"),
keteranganobject.getString("f1"),
keteranganobject.getString("depo_f1"),
keteranganobject.getString("waktu_masuk"),
keteranganobject.getString("f4"),
keteranganobject.getString("depo_f4"),
keteranganobject.getString("waktu_keluar"),
keteranganobject.getString("ket_absensi"));
absensi.add(keterangan);
System.out.println("data:" + keterangan);
}
TableViewAdapter adapter = new TableViewAdapter(absensi, getApplicationContext());
list.setAdapter((ListAdapter) adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
return absensi;
}
}
keteranganmodel.java
package com.example.eis2.Item;
public class keteranganmodel {
private String shift_day;
private String f1;
private String depo_f1;
private String waktu_masuk;
private String f4;
private String depo_f4;
private String waktu_keluar;
private String ket_absensi;
public keteranganmodel(String shift_day, String f1, String depo_f1, String waktu_masuk, String f4, String depo_f4, String waktu_keluar, String ket_absensi) {
this.shift_day = shift_day;
this.f1 = f1;
this.depo_f1 = depo_f1;
this.waktu_masuk = waktu_masuk;
this.f4 = f4;
this.depo_f4 = depo_f4;
this.waktu_keluar = waktu_keluar;
this.ket_absensi = ket_absensi;
}
public String getShift_day() {
return shift_day;
}
public void setShift_day(String shift_day) { this.shift_day = shift_day; }
public String getF1() {
return f1;
}
public void setF1(String f1) {
this.f1 = f1;
}
public String getDepo_f1() {
return depo_f1;
}
public void setDepo_f1(String depo_f1) {
this.depo_f1 = depo_f1;
}
public String getWaktu_masuk() {return waktu_masuk;}
public void setWaktu_masuk(String waktu_masuk) {this.waktu_masuk = waktu_masuk; }
public String getF4() {
return f4;
}
public void setF4(String f4) {
this.f4 = f4;
}
public String getDepo_f4() {
return depo_f4;
}
public void setDepo_f4(String depo_f4) {
this.depo_f4 = depo_f4;
}
public String getWaktu_keluar() {
return waktu_keluar;
}
public void setKet_absensi(String waktu_keluar) {
this.waktu_keluar = waktu_keluar;
}
public String getKet_absensi() {
return ket_absensi;
}
public void setWaktu_keluar(String ket_absensi) {
this.ket_absensi = ket_absensi;
}
}
and here is it's API
<?php
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$send = curl("http://hrd.tvip.co.id/rest_server/api/absensi/index?shift_day=2020-08-24&shift_day_2=2020-08-27&badgenumber=0100028400");
// mengubah JSON menjadi array
$data = json_decode($send, TRUE);
echo "<pre>";
print_r($data);
echo "</pre>";
?>
I tried every time but it always blank or no showing data
the main problem is
2020-08-31 10:18:16.245 11130-11130/com.example.eis2 W/System.err: org.json.JSONException: Value <pre>Array of type java.lang.String cannot be converted to JSONObject
答案1
得分: 2
根据您的 PHP 代码,您正在使用 echo <pre>
,在您的 API 输出中也是如此, 请从您的代码中删除这两个 pre
echo 语句,仅在 PHP 级别上使用它们来调试您的输出。
因此,在您的 PHP 代码中将会是这样的:
$data = json_decode($send, TRUE);
echo $data;
英文:
As per your php code you are doing echo <pre>
, the same is also coming in output of your api, kindly remove both pre
echo statements from your code, use them for debugging your output at php level only.
So in your php code will be like
$data = json_decode($send, TRUE);
echo $data;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论