需要在Android在线数据库上提供建议JSON

时间:2016-04-15 16:43:25

标签: android json database

我正在开发一个android在线数据库项目。我创建了一个可以连接到在线数据库并将信息提供给用户的应用程序。 由于我不太熟悉java和JSON代码,我也浏览互联网和stackoverflow,我在下面找到这个代码。 但我对代码不太了解,我问的是: 一个。我应该把JSON代码放在哪里? 湾这是从在线数据库中的类别表中获取数据的代码吗? C。我可以把我的在线数据库的URL放在哪里?

请帮助我先生。

NB。这是代码:

public class HomeFragment extends Fragment implements InternetConnectionListener, ApiHandler.ApiHandlerListener {

private static final String ARG_SECTION_NUMBER = "section_number";
private final int CATEGORY_ACTION = 1;
private CategorySelectionCallbacks mCallbacks;
private ArrayList<Category> categoryList;
private ListView categoryListView;
private InternetConnectionListener internetConnectionListener;

public HomeFragment() {

}

public static HomeFragment newInstance(int sectionNumber) {
    HomeFragment fragment = new HomeFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    ((HomeActivity) activity).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER));
    try {
        mCallbacks = (CategorySelectionCallbacks) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException("Activity must implement CategorySelectionCallbacks.");
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    categoryListView = (ListView) rootView.findViewById(R.id.categoryListView);
    return rootView;
}

@Override
public void onResume() {
    super.onResume();
    if (UtilMethods.isConnectedToInternet(getActivity())) {
        initCategoryList();
    } else {
        internetConnectionListener = (InternetConnectionListener) HomeFragment.this;
        showNoInternetDialog(getActivity(), internetConnectionListener,
                getResources().getString(R.string.no_internet),
                getResources().getString(R.string.no_internet_text),
                getResources().getString(R.string.retry_string),
                getResources().getString(R.string.exit_string), CATEGORY_ACTION);
    }

}

//! function for populate category list
private void initCategoryList() {

    /**
     * json is populating from text file. To make api call use ApiHandler class
     *
     *  <CODE>ApiHandler apiHandler = new ApiHandler(this, URL_GET_CATEGORY);</CODE> <BR>
     *  <CODE>apiHandler.doApiRequest(ApiHandler.REQUEST_GET);</CODE> <BR>
     *
     * You will get the response in onSuccessResponse(String tag, String jsonString) method
     * if successful api call has done. Do the parsing as the following.
     */

    String jsonString = loadJSONFromAsset(getActivity(), "get_category_id_list");
    try {
        JSONArray jsonArray = new JSONArray(jsonString);
        categoryList = new ArrayList<Category>();

        for (int i = 0; i < jsonArray.length(); i++) {
            Category category = new Category();// buat variabel category

            category.setId(jsonArray.getJSONObject(i).getString(JF_ID));
            category.setTitle(jsonArray.getJSONObject(i).getString(JF_TITLE));
            category.setIconUrl(jsonArray.getJSONObject(i).getString(JF_ICON));

            if (!TextUtils.isEmpty(jsonArray.getJSONObject(i).getString(JF_BACKGROUND_IMAGE))) {
                category.setImageUrl(jsonArray.getJSONObject(i).getString(JF_BACKGROUND_IMAGE));
            }
            categoryList.add(category);
        }

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                categoryListView.setAdapter(new CategoryAdapter(getActivity(), mCallbacks, categoryList));
            }
        });

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

@Override
public void onConnectionEstablished(int code) {
    if (code == CATEGORY_ACTION) {
        initCategoryList();
    }
}

@Override
public void onUserCanceled(int code) {
    if (code == CATEGORY_ACTION) {
        getActivity().finish();
    }
}

//! catch json response from here
@Override
public void onSuccessResponse(String tag, String jsonString) {
    //! do same parsing as done in initCategoryList()
}

//! detect response error here
@Override
public void onFailureResponse(String tag) {

}

//! callback interface listen by HomeActivity to detect user click on category
public static interface CategorySelectionCallbacks {
    void onCategorySelected(String catID, String title);
}

}

1 个答案:

答案 0 :(得分:0)

我相信这段代码来自一个名为finder的应用

您需要更改此

 String jsonString = loadJSONFromAsset(getActivity(), "get_category_id_list");

ApiHandler apiHandler = new ApiHandler(this, "get_category_id_list");
        apiHandler.doApiRequest(ApiHandler.REQUEST_GET);

这将导致ApiHandler.java文件处理您对服务器的请求,而不是调用assets文件夹中的本地JSON文件。当你完成。转到Constants.java文件并将URL_APB_AP的值更改为您的API网址。对调用loadJSONFromAsset函数的所有行执行相同的操作。

希望这会对你有所帮助。