Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用片段在listView中显示mysql数据库中的数据_Android_Mysql_Json_Listview_Android Fragments - Fatal编程技术网

Android 使用片段在listView中显示mysql数据库中的数据

Android 使用片段在listView中显示mysql数据库中的数据,android,mysql,json,listview,android-fragments,Android,Mysql,Json,Listview,Android Fragments,我正在尝试开发我的第一个应用程序,它包含一个导航抽屉。我目前正在尝试从mysql数据库(在xampp上运行)中读取数据,并将其显示在ListView中。我已经阅读了很多教程,但由于我的.java类正在扩展片段(由于NavigationDrawer),所以我无法找到一个有效的解决方案。经过几个小时的阅读和测试,我得出了以下结论(我知道这不是最高尚的方式,但无论如何…)。不幸的是,在emulator中尝试时,应用程序一直停止。在我看来,getData()方法肯定有问题。有人知道为什么吗?非常感谢你的

我正在尝试开发我的第一个应用程序,它包含一个导航抽屉。我目前正在尝试从mysql数据库(在xampp上运行)中读取数据,并将其显示在ListView中。我已经阅读了很多教程,但由于我的.java类正在扩展片段(由于NavigationDrawer),所以我无法找到一个有效的解决方案。经过几个小时的阅读和测试,我得出了以下结论(我知道这不是最高尚的方式,但无论如何…)。不幸的是,在emulator中尝试时,应用程序一直停止。在我看来,getData()方法肯定有问题。有人知道为什么吗?非常感谢你的帮助! 干杯 凯亚

公共类新闻片段扩展片段{
查看我的视图;
ListView lv;
字符串地址=”http://127.0.0.1/android/words.php";
InputStream=null;
字符串行=null;
字符串结果=null;
字符串[]数据;
JSONObject jo=null;
@可空
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、Bundle savedInstanceState){
myView=充气机。充气(右布局。新闻布局,容器,假);
lv=(ListView)myView.findViewById(R.id.lv);
StrictMode.setThreadPolicy((新的StrictMode.ThreadPolicy.Builder().permitNetwork().build());
getData();
ArrayAdapter=newArrayAdapter(getActivity(),android.R.layout.simple\u list\u item\u 1,数据);
低压设置适配器(适配器);
返回myView;
}
私有void getData(){
试一试{
URL=新的URL(地址);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestMethod(“GET”);
is=新的BufferedInputStream(con.getInputStream());
}捕获(例外e){
e、 printStackTrace();
}
//将内容读入字符串
试一试{
BufferedReader br=新的BufferedReader(新的InputStreamReader(is));
StringBuilder sb=新的StringBuilder();
而((line=br.readLine())!=null){
sb.append(行)。append(“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
e、 printStackTrace();
}
//解析JSON数据
试一试{
JSONArray ja=新JSONArray(结果);
数据=新字符串[ja.length()];
对于(int i=0;i
try{
JSONArray ja=新JSONArray(结果);
数据=新字符串[ja.length()];

对于(int i=0;i我找到了解决方案!问题在于“服务器”地址。仿真器似乎不喜欢“localhost”或“127.0.0.1”,因此我不得不将其更改为我的动态IP地址。:-

嘿,谢谢你的帮助!你的解决方案阻止了我的应用程序崩溃。不幸的是,它仍然无法在listView中显示数据:-(
public class newsFragment extends Fragment{

    View myView;
    ListView lv;
    String address = "http://127.0.0.1/android/words.php";
    InputStream is = null;
    String line = null;
    String result = null;
    String[] data;
    JSONObject jo = null;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.news_layout,container,false);

        lv = (ListView)myView.findViewById(R.id.lv);
        StrictMode.setThreadPolicy((new StrictMode.ThreadPolicy.Builder().permitNetwork().build()));

        getData();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,data);
        lv.setAdapter(adapter);

        return myView;
    }

    private void getData(){
        try {
            URL url = new URL(address);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            is = new BufferedInputStream(con.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }

        //READ IS CONTENT INTO A STRING
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            while((line=br.readLine())!=null){
                sb.append(line).append(" \n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //PARSE JSON DATA
        try{
            JSONArray ja = new JSONArray(result);
            data = new String[ja.length()];
            for(int i=0;i<ja.length();i++){
                jo=ja.getJSONObject(i);
                data[i]=jo.getString("name");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
try {
    JSONArray ja = new JSONArray(result);
    data = new String[ja.length()]; 
    for(int i=0;i<ja.length();i++){
        jo=ja.getJSONObject(i);
        data[i]=jo.getString("name");
   }
    //here u pass data to arrayadapter and load to list view..

} catch (Exception e) {
    e.printStackTrace();
}