My php:
<?php
/*
* Created by Spider Lynxz
* Retrieve Data From MySQL Database in Android
*/
//database constants
require_once 'includes/config.php';
// Connecting to mysql database
$conn = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE);
// Call query
$query = "select sales_schedule_name,sales_schedule_date,sales_schedule_time,sales_schedule_address,sales_schedule_description from sales_schedule";
if ($stmt = $conn->prepare($query)) {
$stmt->execute();
$stmt->bind_result($salesschedulename, $salesscheduledate, $salesscheduletime, $salesscheduleaddress, $salesscheduledescription);
if ($stmt > 0) {
$schedule = array();
while ($stmt->fetch()) {
//printf("%s, %s, %s, %s, %s
", $sales_schedule_name, $sales_schedule_date, $sales_schedule_time, $sales_schedule_address, $sales_schedule_description);
$temp = array();
$temp['salesschedulename'] = $salesschedulename;
$temp['salesscheduledate'] = $salesscheduledate;
$temp['salesscheduletime'] = $salesscheduletime;
$temp['salesscheduleaddress'] = $salesscheduleaddress;
$temp['salesscheduledescription'] = $salesscheduledescription;
array_push($schedule, $temp);
}
}else
{
}
$stmt->close();
}
//displaying the result in json format
echo json_encode($schedule);
here of php result:
[
{
salesschedulename: "Visiting Client,",
salesscheduledate: "2018-01-18",
salesscheduletime: "10:30:00",
salesscheduleaddress: "Auckland",
salesscheduledescription: "Sales Visit"
},
{
salesschedulename: "Hunting Client",
salesscheduledate: "2018-01-21",
salesscheduletime: "09:00:00",
salesscheduleaddress: "Sidney",
salesscheduledescription: "Sales Propose"
},
{
salesschedulename: "Visitng Client",
salesscheduledate: "2018-01-25",
salesscheduletime: "09:00:00",
salesscheduleaddress: "Bali, Indonesia",
salesscheduledescription: "Sales Propose"
}
]
the problem is: This data can not show on android emulator
Listview Fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="id.infision.dev.farminformationsystemhybrid.fragment.tabSchedule.ScheduleFragment"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/schedule_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
ScheduleList.Java
public class ScheduleList {
private String ScheduleName;
private String ScheduleDate;
private String ScheduleTime;
private String ScheduleAddress;
private String ScheduleDescription;
public ScheduleList(String scheduleName, String scheduleDate, String scheduleTime, String scheduleAddress, String scheduleDescription) {
ScheduleName = scheduleName;
ScheduleDate = scheduleDate;
ScheduleTime = scheduleTime;
ScheduleAddress = scheduleAddress;
ScheduleDescription = scheduleDescription;
}
public String getScheduleName() {
return ScheduleName;
}
public void setScheduleName(String scheduleName) {
ScheduleName = scheduleName;
}
public String getScheduleDate() {
return ScheduleDate;
}
public void setScheduleDate(String scheduleDate) {
ScheduleDate = scheduleDate;
}
public String getScheduleTime() {
return ScheduleTime;
}
public void setScheduleTime(String scheduleTime) {
ScheduleTime = scheduleTime;
}
public String getScheduleAddress() {
return ScheduleAddress;
}
public void setScheduleAddress(String scheduleAddress) {
ScheduleAddress = scheduleAddress;
}
public String getScheduleDescription() {
return ScheduleDescription;
}
public void setScheduleDescription(String scheduleDescription) {
ScheduleDescription = scheduleDescription;
}
}
last here my ScheduleFragment.java (with sample + json )
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_schedule, container, false);
final ArrayList<ScheduleList> schedule = new ArrayList<>();
try {
JSONArray jsonArray=new JSONArray(getURL(AppConfig.URL_SCHEDULELIST));
//iterate loop
for(int i=0;i<jsonArray.length();i++){
//get the JSON Object
JSONObject schedules=jsonArray.getJSONObject(i);
//prepare data to schedule list
String scheduleName = schedules.getString("salesschedulename");
String scheduleDate = schedules.getString("salesscheduledate");
String scheduleTime = schedules.getString("salesscheduletime");
String scheduleAddress = schedules.getString("salesscheduleaddress");
String scheduleDescription = schedules.getString("salesscheduledescription");
//adding the schedule to schedule list
schedule.add(new ScheduleList(scheduleName, scheduleDate, scheduleTime, scheduleAddress, scheduleDescription));
}
} catch (Exception e) {
System.out.println(e.getMessage());
Toast toast = Toast.makeText(
//getActivity(),"Custom Toast From Fragment",Toast.LENGTH_LONG
getActivity().getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG
);
// Set the Toast display position layout center
toast.setGravity(Gravity.CENTER,0,0);
// Finally, show the toast
toast.show();
}
//sampledata
//schedule.add(new ScheduleList("test5", "test6", "test7", "test8", "test9"));
BindDictionary<ScheduleList> dictionary = new BindDictionary<>();
dictionary.addStringField(R.id.sales_schedule_name, new StringExtractor<ScheduleList>() {
@Override
public String getStringValue(ScheduleList schedule1, int position) {
return schedule1.getScheduleName();
}
});
dictionary.addStringField(R.id.sales_schedule_date, new StringExtractor<ScheduleList>() {
@Override
public String getStringValue(ScheduleList schedule1, int position) {
return schedule1.getScheduleDate();
}
});
dictionary.addStringField(R.id.sales_schedule_time, new StringExtractor<ScheduleList>() {
@Override
public String getStringValue(ScheduleList schedule1, int position) {
return schedule1.getScheduleTime();
}
});
dictionary.addStringField(R.id.sales_schedule_address, new StringExtractor<ScheduleList>() {
@Override
public String getStringValue(ScheduleList schedule1, int position) {
return schedule1.getScheduleAddress();
}
});
dictionary.addStringField(R.id.sales_schedule_description, new StringExtractor<ScheduleList>() {
@Override
public String getStringValue(ScheduleList schedule1, int position) {
return schedule1.getScheduleDescription();
}
});
FunDapter adapter = new FunDapter(ScheduleFragment.this.getActivity(),schedule, R.layout.schedule_list_layout,dictionary);
ListView listView=(ListView) view.findViewById(R.id.schedule_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
ScheduleList selecteditem = schedule.get(position);
Toast.makeText(ScheduleFragment.this.getActivity(),selecteditem.getScheduleName(), Toast.LENGTH_LONG).show();
}
});
return view;
}
public static String getURL(String url) throws Exception {
URL website = new URL(url);
URLConnection connection = website.openConnection();
BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream(),"UTF8"));
StringBuilder response = new StringBuilder();
String inputLine;
String result;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
result=response.toString();
in.close();
return result;
}
And the other problem is, it can show on my device but cant show on other device and emulator. (both local and external php server)
on depedencies i already put: compile 'com.github.amigold.fundapter:library:1.0'
and i already change manifest too.
there is no error found, but on emulator can not show.
So much appreciate to help
here my AppConfig.java
public class AppConfig {
// Server user login url
public static String URL_LOGIN = "http://192.168.43.146/android/login.php";
// Server user register url
public static String URL_REGISTER = "http://192.168.43.146/android/register.php";
// Server user schedule url
public static String URL_SCHEDULELIST = "http://192.168.43.146/android/schedulelist.php";
Server user schedule url
public static String URL_ITEMLIST = "http://192.168.43.146/android/itemlist.php";
}
My android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="id.infision.dev.farminformationsystemhybrid"
xmlns:tool="http://schemas.android.com/tools"
android:versionCode="1"
android:versionName="1.0"
>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="id.infision.dev.farminformationsystemhybrid.permission.MAPS_RECEIVE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<permission
android:name="com.example.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="27" />
<application
android:name=".app.AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tool:replace="android:icon">
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key"
/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"
/>
<activity
android:name=".activity.Splashscreen"
android:label="@string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.LoginActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".activity.RegisterActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".activity.Login_main"
android:label="@string/app_name"
android:launchMode="singleTop" />
<activity
android:name=".activity.MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>