内部服务器错误 [英] Internal Server Error

查看:237
本文介绍了内部服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的短信发送应用程序,并为登录目的,我想使用POST方法从我的Andr​​oid应用程序的Web服务器发送用户名和密码。

当我点击LO-杜松子酒按钮的应用程序没有响应和控制台输出如下消息中的POST请求的响应。

HTTP / 1.1 500内部服务器错误

在运行良好的GET方法我的应用程序。

我无法弄清楚为什么这是造成...

全code是在这里:

 包com.vikas.httplogin;

进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口java.io.InputStreamReader中;
进口的java.util.ArrayList;
进口的java.util.List;

进口org.apache.http.Htt presponse;
进口org.apache.http.NameValuePair;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.entity.UrlEn codedFormEntity;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.message.BasicNameValuePair;

进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.TextView;

公共类HttpLogin延伸活动{
    TextView的电视;

    私有静态最后字符串变量=是fatal_error;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        connecttoServer();
    }

    私人无效connecttoServer()
    {
        的BufferedReader BR = NULL;
        尝试
        {
            HttpClient的客户端=新DefaultHttpClient();
            HttpPost请求=新HttpPost(URL我的网站);

            名单<的NameValuePair> PARAMS =新的ArrayList<的NameValuePair>(3);

            params.add(新BasicNameValuePair(用户名,vikaspatidar));
            params.add(新BasicNameValuePair(密码,patidar));
            UrlEn codedFormEntity实体=新UrlEn codedFormEntity(PARAMS);

           request.setEntity(实体);

          Log.v(标签,request.getMethod()的toString());
          HTT presponse响应= client.execute(要求);


           Log.v(标签,response.getStatusLine()的toString());
            BR =新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent()));
            StringBuffer的SB =新的StringBuffer();
            串线=;
            串NL = System.getProperty(line.separator);
            而((行= br.readLine())!= NULL){
                sb.append(行+ NL);
            }
            br.close();
            字符串结果= sb.toString();
            Log.v(标签,结果);


    }赶上(ClientProtocolException E)
    {

        Log.v(标签,e.getMessage());
    }
     赶上(IllegalStateException异常E){

         Log.v(标签,e.getMessage());
    }赶上(IOException异常E){

         Log.v(标签,e.getMessage());
    }
    }
}
 

解决方案

下面是解决方案,这样的:

<一个href="http://stackoverflow.com/questions/4470936/how-to-do-a-http-post-in-android/4472300#4472300">How做一个HTTP张贴在Android的?

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

解决方案

Here is solution for this:

How to do a HTTP Post in Android?

这篇关于内部服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆