我正在开发一个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 :(得分: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;
}
答案 1 :(得分: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();
}