android中的登录界面出现错误
我试图在android中创建一个简单的登录界面,但是当我尝试运行连接时,出现了错误。我不知道,为什么会这样 这是我的密码:android中的登录界面出现错误,android,login,Android,Login,我试图在android中创建一个简单的登录界面,但是当我尝试运行连接时,出现了错误。我不知道,为什么会这样 这是我的密码: public class MainActivity extends Activity{ String username,password; HttpClient httpclient; HttpPost httppost; HttpResponse response; HttpEntity httpentity; Arra
public class MainActivity extends Activity{
String username,password;
HttpClient httpclient;
HttpPost httppost;
HttpResponse response;
HttpEntity httpentity;
ArrayList<NameValuePair> namevaluepairs;
EditText etUser,etPass;
Button bLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUser=(EditText) findViewById(R.id.etUser);
etPass=(EditText) findViewById(R.id.etPass);
bLogin=(Button) findViewById(R.id.bSubmit);
bLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
httpclient=new DefaultHttpClient();
httppost=new HttpPost("http://192.168.1.107/php_project/checking.php");
username=etUser.getText().toString();
password=etPass.getText().toString();
try {
namevaluepairs=new ArrayList<NameValuePair>();
namevaluepairs.add(new BasicNameValuePair(username, username));
namevaluepairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(namevaluepairs));
response=httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()==200){
httpentity=response.getEntity();
if(httpentity!=null){
InputStream inputstream=httpentity.getContent();
JSONObject jsonResponse=new JSONObject(convertStreamToString(inputstream));
String retUser=jsonResponse.getString("user");
String retPass=jsonResponse.getString("pass");
if(username.equals(retUser) && password.equals(retPass)){
SharedPreferences sp=getSharedPreferences("logindetails", 0);
SharedPreferences.Editor spedit=sp.edit();
spedit.putString("user", username);
spedit.putString("pass", password);
spedit.commit();
Toast.makeText(getApplicationContext(), "SUCCESS !", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Inavalid Login details", Toast.LENGTH_LONG).show();
}
}
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Connection error", Toast.LENGTH_LONG).show();
}
}
});
}
private static String convertStreamToString(InputStream is){
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 e2) {
e2.printStackTrace();
}
}
return sb.toString();
}
}
公共类MainActivity扩展活动{
字符串用户名、密码;
HttpClient-HttpClient;
HttpPost-HttpPost;
HttpResponse响应;
HttpEntity HttpEntity;
ArrayList名称值对;
编辑文本etUser,etPass;
按钮博客;
@凌驾
创建时受保护的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.bSubmit);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
httpclient=新的DefaultHttpClient();
httppost=新的httppost(“http://192.168.1.107/php_project/checking.php");
username=etUser.getText().toString();
password=etPass.getText().toString();
试一试{
namevaluepairs=新的ArrayList();
添加(新的BasicNameValuePair(用户名,用户名));
添加(新的BasicNameValuePair(“密码”,password));
setEntity(新的UrlEncodedFormEntity(namevaluepairs));
response=httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()==200){
httpentity=response.getEntity();
if(httpentity!=null){
InputStream InputStream=httpentity.getContent();
JSONObject jsonResponse=新的JSONObject(convertStreamToString(inputstream));
String retUser=jsonResponse.getString(“用户”);
String retPass=jsonResponse.getString(“pass”);
if(username.equals(retUser)和password.equals(retPass)){
SharedReferences sp=GetSharedReferences(“登录详细信息”,0);
SharedReferences.Editor spedit=sp.edit();
spedit.putString(“用户”,用户名);
spedit.putString(“pass”,密码);
提交();
Toast.makeText(getApplicationContext(),“SUCCESS!”,Toast.LENGTH\u LONG.show();
}否则{
Toast.makeText(getApplicationContext(),“无效登录详细信息”,Toast.LENGTH_LONG.show();
}
}
}
}捕获(例外e){
e、 printStackTrace();
Toast.makeText(getApplicationContext(),“连接错误”,Toast.LENGTH_LONG.show();
}
}
});
}
私有静态字符串convertStreamToString(InputStream为){
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
StringBuilder sb=新的StringBuilder();
字符串行=null;
试一试{
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
is.close();
}捕获(IOE2异常){
e2.printStackTrace();
}
}
使某人返回字符串();
}
}
有人能帮我解决这个问题吗。。
当我在模拟器中运行它时,它会给我Toast消息,即“连接错误”
我简直无法理解,为什么我不能让它成为一个安全登录…?检查您是否在清单中授予了网络连接权限。
Check whether you have given Network Connection permission in manifest.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
在询问此类问题时,请添加日志
看看IP,我猜你想要使用10.0.2.2
在这里阅读更多信息:我已经授予了这些权限,地址:09-06 05:35:48.794:W/System.err(1564):android.os.NetworkOnMainThreadException 09-06 05:35:48.804:W/System.err(1564):在android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)上,用户无法在UI线程中执行与互联网相关的任务。。使用AsynTask调用PHP页面Logcat------09-06 05:35:48.794:W/System.err(1564):android.os.NetworkOnMainThreadException 09-06 05:35:48.804:W/System.err(1564):在android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)上,因此需要在单独的线程中执行网络请求。检查以下答案:谢谢,我有了基本的想法。。。