内部服务器错误

时间:2010-10-02 07:36:41

标签: java android http internal-server-error

我正在处理短信发送应用程序,出于登录目的,我想使用POST方法从我的Android应用程序向网络服务器发送用户名和密码。

当我点击lo-gin按钮时,应用程序没有响应,并且控制台打印以下消息以响应Post请求。

HTTP / 1.1 500内部服务器错误

虽然我的应用程序使用GET方法正常运行。

我无法弄清楚为什么会造成这种情况......

整个代码在这里:

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());
    }
    }
}

2 个答案:

答案 0 :(得分:1)

这肯定看起来像服务器端错误,而不是android问题。查看服务器日志文件。

您为请求设置参数的方式看起来很奇怪,请尝试设置如下参数:

    HttpPost post = new HttpPost("url of my site");
    BasicHttpParams basicHttpParams = new BasicHttpParams();
    basicHttpParams.setParameter("username", "vikaspatidar");
    basicHttpParams.setParameter("password", "patidar");
    post.setParams(basicHttpParams);
    //...

答案 1 :(得分:-1)

以下是解决方案:

How to do a HTTP Post in Android?