从WebService Android Studio获取数据

时间:2018-06-25 07:38:21

标签: java android json listview

我正在开发一个android应用程序。在此应用程序中,我必须从Web服务检索数据并将其放在列表视图中,但它没有检索任何内容。我不知道我在做什么错。也许有人可以帮助我。

网络服务:http://10.247.240.53/kas/lampen.php

主要活动:        

public class MainActivity extends AppCompatActivity {

ListView listView;
ArrayAdapter<String> adapter;
String[] data;


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

    listView = findViewById(R.id.listview);

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());

    getData();

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(MainActivity.this, Showdata.class);
        intent.putExtra("teeltbed", listView.getItemAtPosition(position).toString());
        startActivity(intent);
        }
    });

}

private void getData()
{
    String getData = null;
    String dbResult = "empty";
    dbConnect database = new dbConnect(this);

    try{
        String query = "SELECT * FROM Lamp";
        getData = "?query=" + URLEncoder.encode(query, "UTF-8");
        String link = "http://10.247.240.53/kas/lampen.php";
        dbResult = database.execute(link).get();
    }
    catch (Exception e){

    }

    try{
        JSONArray ja = new JSONArray(dbResult);
        JSONObject jo = null;
        data = new String[ja.length()];
        for (int i = 0; i < ja.length(); i++)
        {
            jo = ja.getJSONObject(i);
            data[i] = jo.getString("teeltbed");
        }
        adapter = new ArrayAdapter<String>(this, R.layout.layout_list, R.id.list_item, data);
        listView.setAdapter(adapter);
    }

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

0 个答案:

没有答案