1

我正在开发一个Android项目,因为我需要将登录详细信息发送到在线数据库。我完成了我的代码如下......
但是当我运行我的项目时,只有else条件正在工作。
没有任何东西传递到数据库

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

            }
        });
    }
}
4

2 回答 2

2

添加参数后尝试此代码:

尝试 {

            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;

}
于 2012-10-10T08:35:06.903 回答
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();
                                    }
于 2012-10-10T08:47:16.183 回答