如何在请求时使用url传递id?

时间:2014-11-10 09:02:45

标签: android

loginpage.java这是我登录页面的活动。

我正在使用这个json {"status":"success","msg":"Your are now Login Successfully","user_login_id":2650}

这里我在用户成功登录时收到一个唯一字段(user_login_id)。它工作正常。

登录后我使用的是Navigationdrawer,它是Activity类,它有不同类型的片段行为,其中一个是HomeFragment,它用Fragment扩展。

在那个HomeFragment类中我想在List视图中获取一些json数据。因为我有另一个json文件。它有以下链接,如http://sdfkjksd.com/apps/matching?version=apps&user_login_id=2650

public class HomeFragment扩展了Fragment {

NavDrawerListAdapter adapters;
private ProgressDialog pDialog;
//JSON parser class
JSONParsing jsonParser = new JSONParsing();

String result = "";

List<HashMap<String,String>> aList;

private static final String TAG_USERID="user_login_id";

private static final String MATCH_URL = "http://sdfa.com/apps/matching?version=apps&user_login_id="+TAG_USERID;




JSONArray matching=null;
private static final String TAG_SUCCESS = "status";
private static final String TAG_MATCH="matching";
private static final String TAG_NAME="name";
private static final String TAG_PROFILE="profile_id";
private static final String TAG_IMAGE="image";
private static final String TAG_CAST="cast";
private static final String TAG_AGE="age";
private static final String TAG_LOCATION="location";

        private ListView listview;
        ArrayAdapter<String> adapter;
public HomeFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    //String strtext = getArguments().getString("user_login_id");

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    aList = new ArrayList<HashMap<String,String>>();
   /* Bundle bundle = this.getArguments();
    if(bundle != null){
        String i = bundle.getString("user_login_id", TAG_USERID);
    }*/



    new LoadAlbums().execute();



    return rootView;
}

class LoadAlbums extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(HomeFragment.this.getActivity());
        pDialog.setMessage("Loading...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("version", "apps"));
        // getting JSON string from URL
        String json = jsonParser.makeHttpRequest(MATCH_URL, "POST",
                params);

        // Check your log cat for JSON reponse
        Log.d("Albums JSON: ", "> " + json);

        try {              

            JSONObject Jasonobject = new JSONObject(result);
            JSONArray Jarray = Jasonobject.getJSONArray("matching");


            if (Jarray != null) {
                // looping through All data
                for (int i = 0; i < Jarray.length(); i++) {
                    JSONObject c = Jarray.getJSONObject(i);

                    // Storing each json item values in variable
                    String user_name = c.getString(TAG_NAME);
                    String user_profile=c.getString(TAG_PROFILE);
                    String user_image=c.getString(TAG_IMAGE);
                    String user_cast=c.getString(TAG_CAST);
                    String user_age=c.getString(TAG_AGE);
                    String user_location=c.getString(TAG_LOCATION);



                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_NAME,user_name);
                    map.put(TAG_PROFILE, user_profile);
                    map.put(TAG_IMAGE, user_image);
                    map.put(TAG_CAST, user_cast);
                    map.put(TAG_AGE, user_age);
                    map.put(TAG_LOCATION, user_location);



                    // adding HashList to ArrayList
                    aList.add(map);
                }
            }else{
                Log.d("Albums: ", "null");
            }

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

        return null;
    }


    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all albums
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                SimpleAdapter adapter = new SimpleAdapter(
                        getActivity().getBaseContext(), aList,
                        R.layout.list_item_matchs, new String[] { TAG_NAME,TAG_PROFILE,TAG_IMAGE,TAG_CAST,TAG_AGE,TAG_LOCATION
                         }, new int[] {
                                R.id.txtproname,R.id.txtprofileage,R.id.propic,R.id.txtprofilecast,R.id.txtprofileplace});

                // updating listview

                listview.setAdapter(adapter);

            }


        });

    }
    private void runOnUiThread(Runnable runnable) {
        // TODO Auto-generated method stub

    }

}

}

所以问题是,在该链接中我使用登录json的user_login_id字段,所以我想在使用URL请求期间发送或传递该字段..

2 个答案:

答案 0 :(得分:0)

我建议您使用 SharedPreference 。当您获得Login响应时,只需解析 JSON 然后获取log_in_id字段值并将其保存在 SharedPreferences 中,然后每当您想要使用此值时,您都可以在整个应用程序中使用

答案 1 :(得分:0)

你已经在doInBackground中使用了相同的方法: 这是一个建议的编辑,为下一个准备它:

  1. 添加&#34; user_login_id&#34; to params list
  2. 如果是,则将POST更改为GET。

    protected String doInBackground(String ... args){         //构建参数         List params = new ArrayList();

        params.add(new BasicNameValuePair("version", "apps"));
    
        // add the user_login_id to params.
       params.add(new BasicNameValuePair("user_login_id ", "265"));
        // getting JSON string from URL 
       // I think you are using GET bcz the url shows the parameters.
        String json = jsonParser.makeHttpRequest(MATCH_URL, "GET",
                params);