Java Genymotion中的代码在Android Studio中不起作用
我的登录代码在genymotion中运行得非常好,但它不会在android studio上运行。在genymotion中,用户名和密码在数据库中找到,我可以登录,而在androidstudio中,它一直说php文件中有语法错误。我已经为所有内容复制粘贴了相同的代码,但它仍然给我相同的错误Java Genymotion中的代码在Android Studio中不起作用,java,php,android,genymotion,Java,Php,Android,Genymotion,我的登录代码在genymotion中运行得非常好,但它不会在android studio上运行。在genymotion中,用户名和密码在数据库中找到,我可以登录,而在androidstudio中,它一直说php文件中有语法错误。我已经为所有内容复制粘贴了相同的代码,但它仍然给我相同的错误 package com.example.login2; import java.util.ArrayList; import org.apache.http.NameValuePair;
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();
}
}
});
}
}
package com.example.login2;
导入java.util.ArrayList;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.graphics.Color;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
公共类login2扩展了活动{
编辑文本un,pw;
文本视图错误;
按钮ok;
受保护字符串macAddress;
受保护的字符串IP地址;
受保护的字符串版本;
/**在首次创建活动时调用*/
//公共无效日志(视图){
//对按钮做些反应
//意向意向=新意向(此为Logout.class);
//星触觉(意向);
// }
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
un=(编辑文本)findViewById(R.id.et_un);
pw=(编辑文本)findViewById(R.id.et_pw);
ok=(按钮)findViewById(R.id.btn\u登录);
错误=(TextView)findViewById(R.id.tv_错误);
macAddress=CustomHttpClient.getMacAddress(getBaseContext());
IPAddress=CustomHttpClient.getIP(getBaseContext());
version=CustomHttpClient.getAndroidVersion();
ok.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
ArrayList后参数=新的ArrayList();
添加(新的BasicNameValuePair(“用户名”,un.getText().toString());
添加(新的BasicNameValuePair(“密码”,pw.getText().toString());
add(新的BasicNameValuePair(“MacAddress”,MacAddress.toString());
添加(新的BasicNameValuePair(“IPAddress”,IPAddress.toString());
添加(新的BasicNameValuePair(“Version”,Version.toString());
//字符串valid=“1”;
字符串响应=null;
试一试{
响应=CustomHttpClient.executeHttpPost(“http://192.168.1.7/thesis/check.php“,后参数);
String res=response.toString();
//res=res.trim();
res=res.replaceAll(“\\s+”,”);
//错误.setText(res);
如果(相对等于(“1”)){
错误。setTextColor(Color.GREEN);
错误.setText(“正确的用户名或密码”);
Toast.makeText(getApplicationContext(),“您已登录”,Toast.LENGTH_LONG.show();
//Intent startMain=新意图(Intent.ACTION\u MAIN);
//startMain.addCategory(Intent.CATEGORY\u HOME);
//startMain.setFlags(意图、标志、活动、新任务);
//星触觉(startMain);
意图=新意图();
intent.setClass(login2.this,Logout.class);
星触觉(意向);
}
否则{
error.setText(“对不起!!用户名或密码不正确”);
Toast.makeText(getApplicationContext(),“对不起,用户名/密码错误”,Toast.LENGTH\u LONG.show();
}
}捕获(例外e){
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}
}
});
}
}
首先,您应该了解Android中的异步任务:
而且,正如您所说的“在android studio中,它一直说php文件中存在语法错误”,该语法错误存在于php文件中,您只是在读取HTTP响应消息
此外,我建议您阅读有关Web服务及其安全实现的内容。询问数字“1或0”并根据该响应进行检查是不安全的