2012-10-10 125 views
1

我正在開發一個Android項目,因爲我需要將登錄詳細信息發送到在線數據庫。我做了我的代碼,如下所示...
但是,當我只運行我的項目其他條件正在工作。
沒有被傳遞到數據庫httpclient類不工作在android

import java.util.ArrayList; 

import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class LoginActivity extends Activity { 
    EditText un,pw,vn; 
    Button ok,reg; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.login); 
     un=(EditText)findViewById(R.id.u_name); 
     pw=(EditText)findViewById(R.id.passwd); 
     vn=(EditText)findViewById(R.id.vh_number); 
     ok=(Button)findViewById(R.id.btnlogin); 
     reg=(Button)findViewById(R.id.btnregister); 

     ok.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
       postParameters.add(new BasicNameValuePair("username", un.getText().toString())); 
       postParameters.add(new BasicNameValuePair("password", pw.getText().toString())); 
       postParameters.add(new BasicNameValuePair("vehiclenumber", vn.getText().toString())); 
       //String valid = "1"; 
       String response = null; 
       try { 

         response = CustomHttpClient.executeHttpPost("http://192.168.1.23/test_login/check.php", postParameters); 
        Log.d("sarath",""); 
        String res=response.toString(); 
        // res = res.trim(); 
        res= res.replaceAll("\\s+","");        
        //error.setText(res); 

        if(res.equals("1")) 
        {          
         Intent i = new Intent(getApplicationContext(), 
           MainActivity.class); 
         startActivity(i); 

        }    
        else 
        { 

         Context context = getApplicationContext(); 
         CharSequence message = "The Username or Password you entered is incorrect.!"; 
         int duration = Toast.LENGTH_SHORT; 
         Toast toast = Toast.makeText(context, message, duration);  
         toast.show(); 
        } 


       } catch (Exception e) { 
        un.setText(e.toString()); 
       } 

      } 
     }); 
    } 
} 
+0

「res」字符串究竟是什麼? – waqaslam

+0

或者更重要的是 - CustomHttpClient在做什麼?你可以發佈源代碼嗎? – Andy

+0

它是來自php輸出的響應字符串,同時檢查數據庫中的用戶名和密碼 – goku

回答

1

試試這個代碼。

DefaultHttpClient hc = new DefaultHttpClient(); 
       ResponseHandler<String> res = new BasicResponseHandler(); 
       HttpPost postMethod = new HttpPost(
          "http://192.168.1.23/test_login/check.php"); 
       ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
       postParameters.add(new BasicNameValuePair("username", un.getText().toString())); 
       postParameters.add(new BasicNameValuePair("password", pw.getText().toString())); 
       postParameters.add(new BasicNameValuePair("vehiclenumber", vn.getText().toString())); 
       try { 

       postMethod 
        .setEntity(new UrlEncodedFormEntity(
           postParameters)); 

       String response = hc.execute(postMethod,res); 
       if(response.equals("1")) 
       {          
        Intent i = new Intent(getApplicationContext(), 
        MainActivity.class); 
        startActivity(i); 
       }    
       else 
        {    
        Context context = getApplicationContext(); 
        CharSequence message = "The Username or Password you entered is incorrect.!"; 
        int duration = Toast.LENGTH_SHORT; 
        Toast toast = Toast.makeText(context, message, duration);  
        toast.show(); 
       } 

      } 
       catch (Exception e) { 
       e.printStackTrace(); 
            } 
+0

中打印行什麼是hc和res? –

+0

你能解釋我什麼是HC和RES? – goku

+0

hc是要使用的DefaultHttpClient的對象從參數和連接管理器創建新的HTTP客戶端。res是ResponseHandler的對象,它將響應正文作爲字符串返回以成功響應。 –

2

試試這個代碼添加perameters後:

嘗試{

  DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(" your url"); 



      ArrayList<NameValuePair> postParameters= new ArrayList<NameValuePair>();      
      postParameters.add(new BasicNameValuePair("username", un.getText().toString())); 
      postParameters.add(new BasicNameValuePair("password", pw.getText().toString())); 
      postParameters.add(new BasicNameValuePair("vehiclenumber", vn.getText().toString())); 


      httpPost.setEntity (new UrlEncodedFormEntity(postParameters)); 



      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      line = EntityUtils.toString(httpEntity); 


     } catch (UnsupportedEncodingException e) { 
      line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; 
     } catch (MalformedURLException e) { 
      line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; 
     } catch (IOException e) { 
      line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; 
     } 

     return line; 

} 
+0

爲此,我已經創建了另一個自定義http客戶端 – goku

+0

行..沒問題。 –

+0

看到我的編輯並嘗試在日誌 –