Android:客户端 - 服务器,用户身份验证

Nit*_*tri 2 authentication android client-server

我从来没有构建任何可以与服务器通信的Android应用程序.我想要做的是我想发送用户名和密码到服务器,在服务器上匹配它们,当用户名和密码匹配下一个屏幕时应该显示.下一个屏幕应该只有一个文本视图说"欢迎用户名".

我希望你们一步一步地告诉我-

  1. 我应该在Android App中写什么?
  2. 我应该在服务器端写什么?
  3. 如何以及在何处编写服务器代码?
  4. 我应该在服务器端使用哪种语言?
  5. 如何在服务器上保存多个用户名密码?

我没有真正的服务器.我将在localhost上运行整个代码.

更新:

这是我在Android应用中编写的内容.我不知道多少是对的.

public void clicked(View v) {
    System.out.println("button clicked");
    EditText username = (EditText) findViewById(R.id.edituser);
    EditText password = (EditText) findViewById(R.id.editpass);

    Editable user = username.getText();
    Editable pass = password.getText();

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.168.1.101:8080//WebApplication1/sendresponse.java");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", user.toString()));
        nameValuePairs.add(new BasicNameValuePair("password", pass.toString()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        Header[] headers = response.getAllHeaders();
        int len = headers.length;
        for(int i=0; i<len; i++) {
            System.out.println("header : " + headers[i]);
        }

        InputStream inputStream = response.getEntity().getContent();

        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        StringBuilder stringBuilder = new StringBuilder();

        String bufferedStrChunk = null;

        while((bufferedStrChunk = bufferedReader.readLine()) != null){
            stringBuilder.append(bufferedStrChunk);
        }
        System.out.println("response : " + stringBuilder.toString());

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }



}
Run Code Online (Sandbox Code Playgroud)

我不知道哪种语言最适合服务器端.我应该在服务器端使用REST吗?

Niz*_*zam 8

我将使用php进行身份验证.首先,

 HttpPost httppost = new HttpPost("http://10.0.2.2/login.php");
Run Code Online (Sandbox Code Playgroud)

10.0.2.2从模拟器中引用localhost.对于真实设备,您需要真正的服务器.

你必须使用phpmyadmin在localhost创建数据库和表.(假设你使用的是WAMP/LAMP)

现在,我的php文件是这样的:

   <?php 
$link=mysql_connect('localhost','root','password');// give your username & password
if (!$link) { 
    die('Could not connect to MySQL: ' . mysql_error()); 
}  
mysql_select_db("database_name"); 
$sql=mysql_query("SELECT * FROM Login_Table_name where user_name = '".$_REQUEST['username']."' and password = '".$_REQUEST['password']."'");
if (mysql_num_rows($sql) > 0) {
 echo "success";
}else{
echo "login failed"; 
}
mysql_close($link); 
?>
Run Code Online (Sandbox Code Playgroud)

(假设您知道将php文件放在何处,对于localhost)(WAMP/LAMP的www文件夹)

现在,回到java,将这些步骤保持原样.并从此处更改代码

HttpResponse response = httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
String res=EntityUtils.toString(entity);
if(res.contains("success")){
//login success
}
else{
//loginfailed
}
Run Code Online (Sandbox Code Playgroud)

现在您可以说如何存储用户名和密码.是的,只需创建另一个注册表单,包装namevaluepairs并发送到另一个php,其中sql查询将更改为"INSERT INTO".

注意:如果您在使用API​​-14 +的模拟器上运行此操作,您将获得异常 - 主线程异常上的网络.你必须使用AsynvTask.所以尝试使用API​​-10.

编辑:

如果您需要从服务器返回一些值,请编码为JSON并发送.变化将是,

PHP

if(mysql_num_rows($sql)>0){
    while($row=mysql_fetch_assoc($sql)){// you can use either fetch_assoc or fetch_array
    $result[]=$row;
    }
echo json_encode($result);
}
Run Code Online (Sandbox Code Playgroud)

Java的

String res=EntityUtils.toString(entity);
Log.d("TAG","From server:"+res);// to see what server sent
if( ! res.contains("login failed")){
JSONArray jArray = new JSONArray(res); 
if(jArray!=null){
            for(int i=0;i<jArray.length();i++){
                JSONObject jobj = jArray.getJSONObject(i);
                //now, you catch the values using column_name. As,
id=jobj.getInt("_id");name= jobj.getString("Name");
//Note that here _id,Name are column names. Refer server result in logcat for better understanding
            }
}

}
Run Code Online (Sandbox Code Playgroud)