从Json File获取特定数据

时间:2016-12-26 07:30:13

标签: php android json listview

以下是我想要特定数据的JSON文件:

    {"android":[{"username":"kripa","company":"natgeo","client":"sandeep","client_no":"8745961587","callback":"2016-12-29 11:30:00"},
    {"username":"daksh","company":"greengold","client":"Suresh","client_no":"5789486589","callback":"2016-12-16 15:14:00"},
    {"username":"usman","company":"HDFC","client":"lokesh","client_no":"8794562153","callback":"2016-12-28 14:00:00"}]}

如果json文件中的用户名是kripa,那么我只需要在Listview中显示它。请帮我... 以下是我的活动文件:

@Override
public void onLoaded(List<AndroidVersion> androidList) {

    for (AndroidVersion android : androidList) {

        HashMap<String, String> map = new HashMap<>();
         map.put(KEY_USERNAME, android.getUsername());
        map.put(KEY_COMPANY, android.getCompany());
        map.put(KEY_CLIENT, android.getClient());
        map.put(KEY_CLIENT_NO, android.getClient_no());
        map.put(KEY_CALLBACK, android.getCallback());

        mAndroidMapList.add(map);
       }        
       }

    loadListView();
}

@Override
public void onError() {

    Toast.makeText(this, "Error !", Toast.LENGTH_SHORT).show();
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    Toast.makeText(this, mAndroidMapList.get(i).get(KEY_CLIENT_NO),Toast.LENGTH_LONG).show();
}

private void loadListView() {

    ListAdapter adapter = new SimpleAdapter(AboutUsActivity.this, mAndroidMapList, R.layout.list_item,
            new String[] { KEY_USERNAME,KEY_COMPANY, KEY_CLIENT, KEY_CLIENT_NO, KEY_CALLBACK },
            new int[] { R.id.username,R.id.company,R.id.client, R.id.client_no,R.id.callback });

    mListView.setAdapter(adapter);

}
  }

下面是LoadJSONTask类:

    public class LoadJSONTask extends AsyncTask<String, Void, Response> {

public LoadJSONTask(Listener listener) {

    mListener = listener;
}

public interface Listener {

    void onLoaded(List<AndroidVersion> androidList);

    void onError();
}

private Listener mListener;

@Override
protected Response doInBackground(String... strings) {
    try {

        String stringResponse = loadJSON(strings[0]);
        Gson gson = new Gson();

        return gson.fromJson(stringResponse, Response.class);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (JsonSyntaxException e) {
        e.printStackTrace();
        return null;
    }
}

@Override
protected void onPostExecute(Response response) {

    if (response != null) {

        mListener.onLoaded(response.getAndroid());

    } else {

        mListener.onError();
    }
}

private String loadJSON(String jsonURL) throws IOException {

    URL url = new URL(jsonURL);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000);
    conn.setConnectTimeout(15000);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    conn.connect();

    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    StringBuilder response = new StringBuilder();

    while ((line = in.readLine()) != null) {

        response.append(line);
    }

    in.close();
    return response.toString();
}
 }

0 个答案:

没有答案