java 内部服务器错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3844813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:38:56  来源:igfitidea点击:

Internal Server Error

javaandroidhttpinternal-server-error

提问by Vikas Patidar

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.

我正在开发 SMS 发送应用程序,出于登录目的,我想使用 POST 方法将用户名和密码从我的 Android 应用程序发送到 Web 服务器。

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.

当我单击登录按钮时,应用程序没有响应,控制台打印以下消息以响应 Post 请求。

HTTP/1.1 500 Internal Server Error

HTTP/1.1 500 内部服务器错误

While my application running fine with the GET method.

虽然我的应用程序使用 GET 方法运行良好。

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

采纳答案by Vikas Patidar

Here is solution for this:

这是对此的解决方案:

How to do a HTTP Post in Android?

如何在 Android 中执行 HTTP Post?

回答by Konstantin Burov

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

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

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);
    //...