我如何从Android上的URL解析JSON并将其显示在listview中

时间:2016-06-23 06:32:14

标签: android json

  1. 我的 JSON 数据如下所示:( JSON 数据有更多数据,但我这里只放了一大块数据。)

     {
     "items": [
        {
          "id": 15,
      "batchId": 0,
     "dateCreated": "2016-05-29T19:03:37Z",
     "description": "sdfdsf",
     "ean": 0,
     "expDate": "1990-12-22T00:00:00Z",
     "itemActive": true,
     "itemAlternateNames": [
      {
      "class": "com.canre.domain.ItemAlternateName",
      "id": 16,
      "dateCreated": "2016-05-29T19:03:37Z",
      "district": null,
      "itemMaster": {
        "_ref": "../..",
        "class": "com.canre.domain.ItemMaster"
      },
      "itemName": "Ryan Reynolds",
      "language": null,
      "lastUpdated": "2016-05-29T19:03:37Z",
      "state": null
       }
      ],
    

    2.这是我的 MainActivity类

    public class MainActivity extends Activity {
    
    ListView listView;
    ItemsAdapter adapter;
    ArrayList<Items> itemsArrayList;
    DBHandler dbHandler;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.listview);
    Button btnHit=(Button) findViewById(R.id.btnHit);
    dbHandler=new DBHandler(this);
    NetworkUtils utils = new NetworkUtils(MainActivity.this);
    if(dbHandler.getItemsCount() == 0 && utils.isConnectingToInternet())
    {
        new DataFetcherTask().execute();
    }
    else
    {
        ArrayList<Items> itemsArrayList = dbHandler.getAllItems();
        adapter=new ItemsAdapter(MainActivity.this,itemsArrayList);
        listView.setAdapter(adapter);
     }
     }
    
    private class DataFetcherTask extends AsyncTask<Void,Void,Void> {
    @Override
    protected Void doInBackground(Void... params) {
        String serverData=null;
    
        String dataUrl =   "my url";
        String dataUrlParameters = "Id:"+"123"+"Updated:"+"dd/mm/yyy";
        URL url;
        HttpURLConnection connection = null;
        try {
    // Create connection
    url = new URL(dataUrl);
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type",    "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Length", "" + Integer.toString(dataUrlParameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US");
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
    // Send request
    DataOutputStream wr = new DataOutputStream(
    connection.getOutputStream());
    wr.writeBytes(dataUrlParameters);
            wr.flush();
            wr.close();
    // Get Response
    InputStream is = connection.getInputStream();                        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();
            String responseStr = response.toString();
            Log.d("Server response", responseStr);
    
        } catch (Exception e) {
    
            e.printStackTrace();
    
        } finally {
    
            if (connection != null) {
                connection.disconnect();
            }
        }
        return null;
    }
    protected void onPostExecute(Void aVoid){
        super.onPostExecute(aVoid);
        ArrayList<Items> itemsArrayList=dbHandler.getAllItems();
        adapter=new ItemsAdapter(MainActivity.this,itemsArrayList);
        listView.setAdapter(adapter);
    }
    }
    }
    
  2. 当我运行项目时,它会抛出 fileNotFound 异常,有时会运行没有错误但是 ListView 上会显示注释....

0 个答案:

没有答案