java List question

Posted on 2012-04-07
Last Modified: 2012-04-12
Hello there,

can somebody please explain this line of code.thanks

List<NameValuePair> params = new ArrayList<NameValuePair>();

Open in new window

Question by:zolf

Author Comment

ID: 37820659
what i am trying to do is

I have these two methods. i am using the executeHttpPost method in the login method to pass 2 parameter.but i get error in the login method when i try to add the String to the List.please help

public String executeHttpPost(List<NameValuePair> data)
			HttpClient client = getHttpClient();
			//String postURL = furl;
			/*Log.d("ZAJ sendPost URL", postURL + " " + data.get(0).toString() + " "
					+ data.get(1).toString());*/
			HttpPost post = new HttpPost(url);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("username", data.get(0).toString()));
			params.add(new BasicNameValuePair("password", data.get(1).toString()));
			UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
			HttpResponse responsePOST = client.execute(post);
			HttpEntity resEntity = responsePOST.getEntity();
			if (resEntity != null)
				String response = EntityUtils.toString(resEntity);
				Log.i("RESPONSE", response);
				return response;

		catch (Exception e)
		return null;

Open in new window


public boolean login(String username,String password)
		 * Log.d("ZAJ URL", url + "?action=login&username=" +
		 * data.get(0).toString() + "&password=" + data.get(1).toString());

		//String fullURL = url + "?action=login";
		List<NameValuePair> data = new ArrayList<NameValuePair>();
		String response = executeHttpPost(data);

		String delimiter = "\\|";

		String[] temp = response.split(delimiter);

		if (temp[0].equals("100"))
			return true;
			return false;

Open in new window

LVL 23

Accepted Solution

basicinstinct earned 200 total points
ID: 37820780
Your list only accepts an instance of "NameValuePair". Is a string an instance of NameValuePair? No. So you can't put a string in it.
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 37821119

What error do you get ?

    Tomas Helgi
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Assisted Solution

Elbassel earned 100 total points
ID: 37821216
This line means that:
The list stores objects of type NameValuePair class, so you can't add any other objects of another type.

Best wishes
LVL 26

Assisted Solution

ksivananth earned 100 total points
ID: 37822458
try this,

data.add(new BasicNameValuePair("username", username));
data.add(new BasicNameValuePair("password", password));
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 100 total points
ID: 37836144
if your using apache http client then you can use name NameValuePair. thats is using messaging purpose .

 ksivananth mentioned correct solution for that

Author Closing Comment

ID: 37841084

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
factorial example 4 40
Configure a Bean in an XML file 4 32
how to install java on RHEL image on EC2 4 28
JavaFX TableView not displaying correctly 3 18
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question