来自php的json无法在android模拟器中的listview上显示

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>      

A B

dongzong8110
dongzong8110 谢谢你的建议。
接近 3 年之前 回复
duan89197
duan89197 你应该添加android-emulator标签,因为你的问题似乎主要与运行时环境(设备与模拟器)有关。
接近 3 年之前 回复

2个回答



在模仿器上的googleplay上更新任何东西后,一切运行良好。
并将其放在主要活动上</ p>

if(android.os.Build.VERSION.SDK_INT&gt; 9){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()。permitAll()。build();
StrictMode.setThreadPolicy (政策);
}
</ code> </ pre>

感谢大家(SuperFADX,Hansa等)</ p>
</ div>

展开原文

原文

after update anything on googleplay at emulator, all running well. and put this on main activity

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

Thanks for all of you (SuperFADX, Hansa and other)

douzhao2047
douzhao2047 好像你的问题已经解决了。 如果适用,请考虑接受答案,以便其他人一眼就知道您的问题已得到解决。
接近 3 年之前 回复



ScheduleFragment.java </ code> </ p>

更改此行:< / p>

  JSONArray jsonArray = new JSONArray(getURL(AppConfig.URL_SCHEDULELIST));

</ code> </ pre>

使用此文件:</ p>

  String page = new Communicator()。executeHttpGet(AppConfig.URL_SCHEDULELIST);

JSONArray jsonArray = new JSONArray(页面);
</ code> </ pre>

并尝试在模拟器的Web浏览器中手动打开您的URL,以检查它是否正确加载。</ p>
</ div>

展开原文

原文

In your ScheduleFragment.java

Change this line :

JSONArray jsonArray=new JSONArray(getURL(AppConfig.URL_SCHEDULELIST)); 

With this one :

String page = new Communicator().executeHttpGet(AppConfig.URL_SCHEDULELIST); 
JSONArray jsonArray = new JSONArray(page);

And try to open manually your URL in the web browser of your Emulator to check if it loads correctly.

doonbfez815298
doonbfez815298 现在你可以检查新的图片上传先生。 我已经阻止了样本数据,但没有显示。 你能解释一下这个comunicator因为我发现了它的错误。
接近 3 年之前 回复
drl47263
drl47263 尝试删除此行,运行并通知我:schedule.add(新的ScheduleList(“test5”,“test6”,“test7”,“test8”,“test9”)); //删除此行后的结果截图也很棒
接近 3 年之前 回复
doutangguali32556
doutangguali32556 你最好检查我的照片。 它上面有LTE符号。
接近 3 年之前 回复
duanfu1873
duanfu1873 down vote accept尝试删除这一行:schedule.add(new ScheduleList(“test5”,“test6”,“test7”,“test8”,“test9”)); 它唯一的样本数据。 要么删除与否,我的模拟器不会在我的列表视图上显示json文件。
接近 3 年之前 回复
dongqiangteng7319
dongqiangteng7319 我的模拟器上有互联网。 有时我常常打开我的Gmail。
接近 3 年之前 回复
donglin317704291
donglin317704291 仍然没有发生。 我让你看看我的清单
接近 3 年之前 回复
douduan2272
douduan2272 我已经编辑了我的问题。 你可以看到我在我的localhost上运行的AppConfig.java。 本地和外部主机具有相同的结果。 无法在其他设备和模拟器上显示。 但在我的设备上显示。
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问