0

I am performing login task and getting data from PHP server in json format. In response, I am getting a 'success' tag that containing User-ID

like this {"message":"You have been successfully login","success":"75"}

I get that value as "uid" in the same activity and move to next page. Now in next page, I want to check user profile. For that, I have to pass that "uid" as 'params' with url and get value from server. But don't understand how to do that.

In next activity page I am creating asyncTask to perform action.

protected String doInBackground(String... args) { // Building Parameters List<NameValuePair> params = new ArrayList<NameValuePair>(); // getting JSON string from URL JSONObject json = jsonParser.makeHttpRequest(PROFILE_URL, "GET",params); // Check your log cat for JSON reponse Log.d("Profile JSON: ", json.toString()); try { // profile json object profile = json.getJSONObject(TAG_PROFILE); } catch (JSONException e) { e.printStackTrace(); } return null; } 

Now I have to set the 'uid' in the place of params.

3
  • Do you need to pass data between activities or fragments? Commented Apr 29, 2015 at 12:43
  • You can pass data between activities by Intent. developer.android.com/reference/android/content/Intent.html Commented Apr 29, 2015 at 12:45
  • @LochanaTejas between activities. from first activity to second activity. In second activity I have a tab button to check profile details. here I have to post that 'uid' with url and get the data. Commented Apr 29, 2015 at 12:45

6 Answers 6

2

Use intent to pass data,

 Intent intent = new Intent(getBaseContext(), SignoutActivity.class); intent.putExtra("UID", uId); startActivity(intent) 
Sign up to request clarification or add additional context in comments.

Comments

1

Use intent, but if you want to keep your uid for a long time , you can use SharedPrefferences

Comments

1

Method 1:

 Class A { String UID = "3"; public static void main(String[] args){ ClassB.setUid(3); } } Class B { public static String uid; public static setUid(String id){ uid = id; } } 

Method 2:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class); intent.putExtra("U_ID", uId); startActivity(intent) 

Beware about static variables, programmers dont usually like them and call them evil.

4 Comments

I am getting value in first Activity like this.. resp = (String) json.get(TAG_SUCCESS); uid = resp.toString(); Log.d("resid", uid);
Which variable do you want to pass to the next activity out of the above?
I want to pass uid in next activity and use that as params to get respective data of that profile.
Use intent.putExtra("uid", uID);
0

If what you're trying to do is transfer data from one activity to another, try adding an extra to your intent. After you've created the intent to launch the next page, add something like

intent.putExtra("uid", uid); 

to add the uid as an extra. And on the next page, you can retrieve this data by

 Intent intent = getIntent(); int uid = intent.getIntExtra("uid", defaultvalue); 

1 Comment

I tried, I got the value in second activity too. but I have a button to check profile details in second activity. when I click that button, it will take me to the profile activity of user where I can see the details of the user. Now the point is, the UID value I am getting from login page and sending to second activity, I have to send that value to the server with url so that i can get user details. I am calling that url in profile activity.
0

In case you need to pass parameters along with GET method, you can simply add the respective values to the url:

public void getData(String uid) { HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet("http://www.yoursite.com/script.php?uid=" + uid); HttpResponse response = httpclient.execute(httpget); } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } } 

If you want to pass parameters with POST method the code is a bit more complex:

public void postData(String uid) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php"); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>; nameValuePairs.add(new BasicNameValuePair("uid", uid)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } } 

In both cases you can get server response as stream:

InputStream s = response.getEntity().getContent(); 

The easiest way to get response body as a String is to call:

String body = EntityUtils.toString(response.getEntity()); 

Of course, there are numerous other ways to achive what you desire.

1 Comment

just call postData(uid); from doInBackground()
0

There are two ways to pass parameters with Get request.

  1. http://myurl.com?variable1=value&variable2=value2
  2. Passing arguments as headers in request.

As HttpClient is now deprecated in the API 22, so you should use the Google Volley https://developer.android.com/training/volley/simple.html

Adding parameters using volley library as

/** * This method is used to add the new JSON request to queue. * @param method - Type of request * @param url - url of request * @param params - parameters * @param headerParams - header parameters * @param successListener - success listener * @param errorListener - error listener */ public void executeJSONRequest(int method, String url, final JSONObject params, final HashMap<String, String> headerParams, Response.Listener successListener, Response.ErrorListener errorListener) { JsonObjectRequest request = new JsonObjectRequest(method, url, params, successListener, errorListener) { @Override public Map<String, String> getHeaders() throws AuthFailureError { if (headerParams != null) return headerParams; else return super.getHeaders(); } }; // Add request to queue. addRequestToQueue(request); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.