Question

try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://nayyar.5gbfree.com/welcome.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost); 
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

                Log.e("log_tag", "connection success ");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
           }

I am trying to connect to server for the very first time. i copied the code from internet trying to execute. but always showing error on nameValuePairs. i don't know what is this. and why the error is. anybody explain me the code and the error or explain me to connect to server in his/her own way via code. Shall be very thankful.

Was it helpful?

Solution

You have to make a NameValuePair list... like this

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://nayyar.5gbfree.com/welcome.php");
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
                    postParameters);
httppost.setEntity(formEntity);
HttpResponse response = httpclient.execute(httppost); 

and then get the data in this way

BufferedReader br = new BufferedReader(new InputStreamReader(
                    httppost.getEntity().getContent()));

br.readLine(); // Save it in a String variable or whatever you want 

OTHER TIPS

What data must receive your POST? for example if the url needs "id" and "user":

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://nayyar.5gbfree.com/welcome.php");

            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("id", "12345"));
            nameValuePairs.add(new BasicNameValuePair("user", "Jorgesys!"));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    ...
    ...
    ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top