5

I am working on an SMS Sending application and for login purpose i want to send the username and password using POST method from my Android Application to the web server.

When I click on lo-gin button the application is not responding and the console prints the following message in response of the Post request.

HTTP/1.1 500 Internal Server Error

While my application running fine with the GET method.

I am not able to figure out why this is causing...

the whole code is here:

package com.vikas.httplogin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class HttpLogin extends Activity {
    TextView tv;

    private static final String tag ="FATAL_ERROR";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        connecttoServer();
    }

    private void connecttoServer()
    {
        BufferedReader br = null;
        try
        {
            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost("url of my site");

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

            params.add(new BasicNameValuePair("username","vikaspatidar"));
            params.add(new BasicNameValuePair("password", "patidar"));
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params);

           request.setEntity(entity);

          Log.v(tag,request.getMethod().toString());
          HttpResponse response = client.execute(request);


           Log.v(tag, response.getStatusLine().toString());
            br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = br.readLine()) != null) {
                sb.append(line + NL);
            }
            br.close();
            String result = sb.toString();
            Log.v(tag, result);


    } catch (ClientProtocolException e)
    {

        Log.v(tag, e.getMessage());
    }
     catch (IllegalStateException e) {

         Log.v(tag, e.getMessage());
    } catch (IOException e) {

         Log.v(tag, e.getMessage());
    }
    }
}
dakshbhatt21
  • 3,558
  • 3
  • 31
  • 40
Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106

2 Answers2

1

That's definitely looks like server-side error, not android problem. Look at server log files.

The way you're setting parameters to request looks weird, try setting parameters like this:

    HttpPost post = new HttpPost("url of my site");
    BasicHttpParams basicHttpParams = new BasicHttpParams();
    basicHttpParams.setParameter("username", "vikaspatidar");
    basicHttpParams.setParameter("password", "patidar");
    post.setParams(basicHttpParams);
    //...
Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
  • I am getting this strange error because the post method parameter's has null values on the server, i don't no why this parameters are not adding in this post method, – Vikas Patidar Oct 02 '10 at 09:27
  • can u please tell me why this is causing? – Vikas Patidar Oct 02 '10 at 09:27
  • Hi Konstantin Burov, Thank you very much for your response, Now at least the server error has been removed and application is responding quick but again the parameters in post method on server are null, please help me.. and please can you tell why the previous method of setting up the parameters in post method is looks weird??? I did read this method in the book "Pro Android 2.0- Apress" and on the other resources on Internet. – Vikas Patidar Oct 02 '10 at 11:51
  • I have never read the books nor seen such a code before. But but from my perspective using BasicParameters is more straightforward way. I think you may need to pass entity only if dealing with binary data. – Konstantin Burov Oct 02 '10 at 12:55
  • Finally traditional Java methods works fine for me.... – Vikas Patidar Oct 15 '10 at 09:16
-1

Here is solution for this:

How to do a HTTP Post in Android?

Community
  • 1
  • 1
Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106