2010-10-02 357 views
5

我正在開發一個SMS發送應用程序,並且爲了登錄目的,我想使用POST方法從我的Android應用程序向Web服務器發送用戶名和密碼。內部服務器錯誤

當我點擊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()); 
    } 
    } 
} 

回答

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

我得到這個奇怪的錯誤,因爲post方法參數在服務器上有空值,我不是沒有爲什麼這個參數沒有在這個post方法中加入, – 2010-10-02 09:27:18

+0

可以請你告訴我爲什麼這是造成的? – 2010-10-02 09:27:49

+0

嘗試使用代碼片段。 – 2010-10-02 09:40:38