1

私は Android の初心者で、データベースを使用して名前をスワイプ ビューに出力したいと考えています。Android 開発者の Web サイト ( http://developer.android.com/training/implementing-navigation/lateral.html ) からサンプル プロジェクトをダウンロードしました。AsyncTask を使用してデータベースを呼び出すコードを変更しようとしましたが、成功しませんでした。これを行う方法はありますか?私が抱えている主な問題は、makehttprequest がフラグメント内 (asynctask から doinbackground 内) で機能しないことです。

public class DemoObjectFragment extends Fragment {

    public static final String ARG_OBJECT = "object";
    private static final String url ="http://www.golinkr.net";
    private ProgressDialog pDialog;
    JSONParser jsonParser = new JSONParser();

    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PROFILE_INFO = "Profile_Info";
    private static final String TAG_FIRST_NAME = "First_Name";
    private static final String SELECT_FUNCTION = "getProfile";
    private static final String ID = "2";


    JSONArray profileInfos = null;
    HashMap<String,String> profile;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_collection_object, container, false);
        profile = new HashMap<String, String>();
        bindGridview();
        return rootView;
    }

    public void bindGridview(){
        new MyAsyncTask(getActivity()).execute("");
    }

    class MyAsyncTask extends AsyncTask<String, String, String> {
        Activity mContex;

        public MyAsyncTask(Activity contex) {
            this.mContex=contex;
        }

        @Override
        protected String doInBackground(String... strings) {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("SELECT_FUNCTION",SELECT_FUNCTION));
            params.add(new BasicNameValuePair("ID", ID));
            JSONObject json = jsonParser.makeHttpRequest(url,"POST",params);

            // Making a request to url and getting response
            String jsonStr = json.toString();
            Log.d("Response: ", "> " + jsonStr);

            //if (jsonStr != null) {
            try {
                int success = json.getInt(TAG_SUCCESS);
                if (success==1){
                    profileInfos=json.getJSONArray(TAG_PROFILE_INFO);
                    for (int i = 0; i<profileInfos.length();i++){
                        JSONObject c = profileInfos.getJSONObject(i);
                        String name = c.getString(TAG_FIRST_NAME)

                        profile.put(TAG_NAME,name);

                    }
                }
                else{
                    Intent i = new Intent(getApplicationContext(),DemoObjectFragment.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }
            }
            catch (JSONException e){
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result){
            TextView txt = (TextView) mContex.findViewById(R.id.text1);
            txt.setText("Bonjour");
        }
    }
}

}

前もって感謝します。質問が以前の質問と似ている場合は申し訳ありません

4

0 に答える 0