它显示"登录失败!"当我用android登录数据库时。 请看一下。
代码点火器数据库代码
<?php
class api扩展了CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('m_activity', '', TRUE);
$this->load->model('m_project', '', TRUE);
}
public function index() {
$this->load->view('api/recentactivities');
}
public function stud_login($std_id, $std_pass){
// $std_id = $this->input->post('std_id');
// $std_pass = $this->input->post('std_pass');
$this->db->select('*');
$this->db->from('student');
$this->db->where('std_id',$std_id);
$this->db->where('std_pass',$std_pass);
$query = $this->db->get();
$result = $query->result_array();
$data['android'] = $result;
echo json_encode($data);
}
}
和我的java控制器页面
公共类登录页面扩展了活动{
public static final String url = "http://ss.data.com/api/std_login";
public static String result = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button btn_std = (Button) findViewById(R.id.btn_std);
btn_std.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
WebPageTask task = new WebPageTask();
task.execute();
}
});
}
public String login(){
EditText edittext_user = (EditText)findViewById(R.id.editText);
EditText edittext_pass = (EditText)findViewById(R.id.editText2);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("std_id",
edittext_user.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("std_pass",
edittext_pass.getText().toString()));
return postData(url,nameValuePairs);
}
public String postData(String url,List<NameValuePair> nameValuePairs){
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
BufferedReader in = null;
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String page = sb.toString();
return page;
}catch (ClientProtocolException e) {
//TODO Auto-generated catch block
}catch (IOException e){
//TODO Auto-generated catch block
}
return null;
}
private class WebPageTask extends AsyncTask<Void, Void, String>{
@Override
protected String doInBackground(Void... param){
result = login();
return result;
}
@Override
protected void onPostExecute(String result){
if (result.trim().equals("OK")){
Toast.makeText(Loginpage.this, "Login Success.",
Toast.LENGTH_LONG).show();
}else {
Toast.makeText(Loginpage.this, "Login Fail.",
Toast.LENGTH_LONG).show();
}
}
}
} 请帮帮我。我试了整整一天。它不再起作用了。
答案 0 :(得分:0)
这是因为你的代码的这一部分:
public String login(){
EditText edittext_user = (EditText)findViewById(R.id.editText);
EditText edittext_pass = (EditText)findViewById(R.id.editText2);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("std_id",
edittext_user.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("std_pass",
edittext_pass.getText().toString()));
return postData(url,nameValuePairs);
你已经宣布了你的名字.ValuePairs.add&#34;因此,如果你明白我的意思,你必须在&#34; pass.getText&#34;之前背诵你的editText变量。
换句话说,试试这个:
nameValuePairs.add(new BasicNameValuePair("std_id",
edittext_user.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("std_pass",
//recite text from intent
var<openTextEditor.class> {
insert.editText.java;
//close snippet
return true
}
edittext_pass.getText().toString()));