【问题标题】:Code in Genymotion does not work in Android StudioGenymotion 中的代码在 Android Studio 中不起作用
【发布时间】:2015-06-09 12:10:45
【问题描述】:

我的登录代码在 genymotion 中运行良好,但无法在 android studio 上运行。在 genymotion 中,他的用户名和密码在数据库中找到,我可以登录,而在 android studio 中它一直说 php 文件中有语法错误。我已经为所有内容复制粘贴了相同的代码,但它仍然给我同样的错误。

  package com.example.login2;


 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.graphics.Color;
 import android.os.Bundle;
 import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class login2 extends Activity {
EditText un,pw;
TextView error;
Button ok;
protected String macAddress;
protected String IPAddress;
protected String version;
/** Called when the activity is first created. */

// public void LoggedIn(View view) {
// Do something in response to button
//       Intent intent = new Intent(this, Logout.class);
//       startActivity(intent);
//  }

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    un=(EditText)findViewById(R.id.et_un);
    pw=(EditText)findViewById(R.id.et_pw);
    ok=(Button)findViewById(R.id.btn_login);
    error=(TextView)findViewById(R.id.tv_error);
    macAddress = CustomHttpClient.getMacAddress(getBaseContext());
    IPAddress = CustomHttpClient.getIP(getBaseContext());
    version = CustomHttpClient.getAndroidVersion();




    ok.setOnClickListener(new View.OnClickListener() {

        @Override
        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("MacAddress",macAddress.toString()));
            postParameters.add(new BasicNameValuePair("IPAddress",IPAddress.toString()));
            postParameters.add(new BasicNameValuePair("Version",version.toString()));
            //String valid = "1";
            String response = null;
            try {
                response = CustomHttpClient.executeHttpPost("http://192.168.1.7/thesis/check.php", postParameters);
                String res=response.toString();
                // res = res.trim();
                res= res.replaceAll("\\s+","");
                //error.setText(res);

                if(res.equals("1")){
                    error.setTextColor(Color.GREEN);
                    error.setText("Correct Username or Password");
                    Toast.makeText(getApplicationContext(), "You Logged in ", Toast.LENGTH_LONG).show();
                    //Intent startMain = new Intent(Intent.ACTION_MAIN);
                    //startMain.addCategory(Intent.CATEGORY_HOME);
                    //startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    // startActivity(startMain);
                    Intent intent = new Intent();
                    intent.setClass(login2.this, Logout.class);
                    startActivity(intent);

                }
                else{
                    error.setText("Sorry!! Incorrect Username or Password");
                    Toast.makeText(getApplicationContext(), "Sorry, wrong Username/Password", Toast.LENGTH_LONG).show();
                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();


            }

        }
    });
}
}

【问题讨论】:

    标签: java php android genymotion


    【解决方案1】:

    首先你应该了解一下 Android 中的异步任务:

    Android HTTP Request AsyncTask

    http://loopj.com/android-async-http/

    而且,正如您所说的“在 android studio 中,它一直说 php 文件中存在语法错误”,该语法错误存在于您的 php 文件中,而您只是在阅读 HTTP 响应消息。

    此外,我建议您阅读有关 Web 服务及其安全实施的信息。询问数字“1 或 0”并对照该响应进行检查并不安全。

    Secure Web Services: REST over HTTPS vs SOAP + WS-Security. Which is better?

    https://developer.android.com/training/articles/security-ssl.html

    【讨论】:

      猜你喜欢
      • 2017-08-19
      • 2020-12-05
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 2019-06-11
      • 2019-06-23
      • 2020-08-12
      • 1970-01-01
      相关资源
      最近更新 更多