Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 仍然无法运行连接到数据库的登录应用程序。我申请了这项任务_Android - Fatal编程技术网

Android 仍然无法运行连接到数据库的登录应用程序。我申请了这项任务

Android 仍然无法运行连接到数据库的登录应用程序。我申请了这项任务,android,Android,仍然无法运行连接到数据库的登录应用程序。我申请了这项任务,但仍然没有成功。能帮我查一下这个吗。提前谢谢。首先,我一单击run按钮就会出现这个错误。你能帮我查一下密码并告诉我怎么了吗。提前谢谢 public class MainActivity extends Activity implements OnClickListener { EditText etUser, etPass; Button bLogi

仍然无法运行连接到数据库的登录应用程序。我申请了这项任务,但仍然没有成功。能帮我查一下这个吗。提前谢谢。首先,我一单击run按钮就会出现这个错误。你能帮我查一下密码并告诉我怎么了吗。提前谢谢

            public class MainActivity extends Activity implements OnClickListener {

                EditText etUser, etPass;
                Button bLogin;

                //Create string variables that will have the input assigned to them
                String username, password;

                //Create a HTTPClient as the form container
                HttpClient httpclient;

                //Use HTTP POST method
                HttpPost httppost;

                //Create an array list for the input data to be sent
                ArrayList<NameValuePair> nameValuePairs;

                //Create a HTTP Response and HTTP Entity
                HttpResponse response;
                HttpEntity entity;


                @Override
                public void onCreate(Bundle savedInstanceState) {

                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);

                    initialise();

                }



                private void initialise() {
                    etUser = (EditText) findViewById(R.id.etUser);
                    etPass = (EditText) findViewById(R.id.etPass);
                    bLogin = (Button) findViewById(R.id.etSubmit);
                    //Now to set an onClickListener
                    bLogin.setOnClickListener((android.view.View.OnClickListener) this);
                }

                public void onClick(View v)  {
                    // This is where we will be working now

                    new MyAsyncTask().execute();

                }//END onClick()

                private static String convertStreamToString(InputStream is) {
                    /*
                     * To convert the InputStream to String we use the BufferedReader.readLine()
                     * method. We iterate until the BufferedReader return null which means
                     * there's no more data to read. Each line will appended to a StringBuilder
                     * and returned as String.
                     */
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    try {
                        while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    return sb.toString();
                }//END convertStreamToString()



                private class MyAsyncTask extends AsyncTask<Void, Void, Void>
            {
                    ProgressDialog mProgressDialog;
                    @Override
                    protected void onPostExecute(Void result) {
                        mProgressDialog.dismiss();
                    }

                    @Override
                    protected void onPreExecute() {
                        mProgressDialog = ProgressDialog.show(MainActivity.this, "Loading...", "Data is Loading...");
                    }

                    @Override
                    protected Void doInBackground(Void... params) {

                        //Create new default HTTPClient
                        httpclient = new DefaultHttpClient();

                        //Create new HTTP POST with URL to php file as parameter
                        httppost = new HttpPost("http://10.0.2.2/myteamapp/index.php"); 

                        //Assign input text to strings
                        username = etUser.getText().toString();
                        password = etPass.getText().toString();



                        //Next block of code needs to be surrounded by try/catch block for it to work
                        try {
                            //Create new Array List
                            nameValuePairs = new ArrayList<NameValuePair>(2);

                            //place them in an array list
                            nameValuePairs.add(new BasicNameValuePair("user", "username"));
                            nameValuePairs.add(new BasicNameValuePair("pass", "password"));

                            //Add array list to http post
                            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


                            //assign executed form container to response
                            response = httpclient.execute(httppost); //response from the PHP file

                            //check status code, need to check status code 200
                            if(response.getStatusLine().getStatusCode() == 200){

                                //assign response entity to http entity
                                entity = response.getEntity();

                                //check if entity is not null
                                if(entity != null){


                                    //Create new input stream with received data assigned
                                    InputStream instream = entity.getContent();

                                    //Create new JSON Object. assign converted data as parameter.
                                    JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));

                                    //assign json responses to local strings
                                    String retUser = jsonResponse.getString("user");//mySQL table field
                                    String retPass = jsonResponse.getString("pass");

                                    //Validate login
                                    if(username.equals(retUser)&& password.equals(retPass)){ //Check whether 'retUser' and 'retPass' matches username/password 

                                        //Display a Toast saying login was a success
                                        Toast.makeText(getBaseContext(), "Successful", Toast.LENGTH_SHORT).show();


                                    } else {
                                        //Display a Toast saying it failed.

                                        Toast.makeText(getBaseContext(), "Invalid Login Details", Toast.LENGTH_SHORT).show();
                                    }

                                }


                            }


                        } catch(Exception e){

                           // e.printStackTrace();
                            //Display toast when there is a connection error
                            //Change message to something more friendly
                           Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_SHORT).show();
                           Toast.makeText(getBaseContext(), "Connection Error", Toast.LENGTH_SHORT).show();

                           return null;
                        }



                        return null;
                    }
                }



                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            }
public类MainActivity扩展活动实现OnClickListener{
编辑文本etUser,etPass;
按钮博客;
//创建将输入分配给它们的字符串变量
字符串用户名、密码;
//创建一个HTTPClient作为表单容器
HttpClient-HttpClient;
//使用HTTP POST方法
HttpPost-HttpPost;
//为要发送的输入数据创建数组列表
ArrayList名称值对;
//创建HTTP响应和HTTP实体
HttpResponse响应;
http实体;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
初始化();
}
私有无效初始化(){
etUser=(EditText)findViewById(R.id.etUser);
etPass=(EditText)findViewById(R.id.etPass);
bLogin=(按钮)findviewbyd(R.id.etSubmit);
//现在设置onclick侦听器
setOnClickListener((android.view.view.OnClickListener)this);
}
公共void onClick(视图v){
//这就是我们现在工作的地方
新建MyAsyncTask().execute();
}//结束onClick()
私有静态字符串convertStreamToString(InputStream为){
/*
*要将InputStream转换为字符串,我们使用BufferedReader.readLine()命令
*我们迭代直到BufferedReader返回null,这意味着
*没有更多的数据可读取。每行都将附加到StringBuilder
*并作为字符串返回。
*/
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
StringBuilder sb=新的StringBuilder();
字符串行=null;
试一试{
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
is.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
使某人返回字符串();
}//END convertStreamToString()
私有类MyAsyncTask扩展了AsyncTask
{
进程对话框;
@凌驾
受保护的void onPostExecute(void结果){
mProgressDialog.disclose();
}
@凌驾
受保护的void onPreExecute(){
mProgressDialog=ProgressDialog.show(MainActivity.this,“正在加载…”,数据正在加载…”);
}
@凌驾
受保护的Void doInBackground(Void…参数){
//创建新的默认HTTPClient
httpclient=新的DefaultHttpClient();
//以php文件的URL作为参数创建新的HTTP POST
httppost=新的httppost(“http://10.0.2.2/myteamapp/index.php"); 
//将输入文本指定给字符串
username=etUser.getText().toString();
password=etPass.getText().toString();
//下一个代码块需要被try/catch块包围,才能正常工作
试一试{
//创建新数组列表
nameValuePairs=新的ArrayList(2);
//将它们放入数组列表中
添加(新的BasicNameValuePair(“用户”、“用户名”);
添加(新的BasicNameValuePair(“pass”、“password”);
//将数组列表添加到http post
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//将已执行的表单容器分配给响应
response=httpclient.execute(httppost);//来自PHP文件的响应
//检查状态代码,需要检查状态代码200
if(response.getStatusLine().getStatusCode()==200){
//将响应实体分配给http实体
entity=response.getEntity();
//检查实体是否不为空
如果(实体!=null){
//使用已分配的接收数据创建新的输入流
InputStream instream=entity.getContent();
//创建新的JSON对象。将转换后的数据指定为参数。
JSONObject jsonResponse=新的JSONObject(convertStreamToString(instream));
//将json响应分配给本地字符串
圣
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.bLogin:
        new MyAsyncTask().execute();
        break;
    default:
        break;
    }}
 Toast.makeText(getBaseContext(), "Successful", Toast.LENGTH_SHORT).show();
 Toast.makeText(getBaseContext(), "Invalid Login Details", Toast.LENGTH_SHORT).show(); 
 Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_SHORT).show();
 Toast.makeText(getBaseContext(), "Connection Error", Toast.LENGTH_SHORT).show();