سلام . ببخشید دوستان یه برنامه دارم برای لاگین شدن از طریق سرور که یوزر و پسورد رو از کاربر میگیره و با سرور ارتباط میگیره اگه یوزر و پسورد توی sql سمت سرور موجود باشه یه پیغام ok برای کاربر ارسال میشه . حالا فایل php سمت سرور مشکلی نداره و درست کار میکنه ولی سمت اندرویدم مشکل داره و با سمت سرور ارتباط نمیگیره . کد برنامه رو میزارم اگه دوستان لطف کنن یه راهنمایی بکنن که مشکلشو پیدا کنم ممنون میشم.
کد برنامه :
کد کلاس :
کد برنامه :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | package com.login.login; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends Activity { public static String r= "" ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText user= (EditText) findViewById(R.id.editText); final EditText password= (EditText) findViewById(R.id.editText2); Button btn= (Button) findViewById(R.id.button); btn.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { login(user.getText().toString(),password.getText().toString()); } }); } private void login(String user,String password){ final ProgressDialog c= new ProgressDialog(MainActivity. this ); c.setMessage( "login..." ); c.show(); final Timer t= new Timer(); t.scheduleAtFixedRate( new TimerTask() { @Override public void run() { runOnUiThread( new Runnable() { @Override public void run() { if (r.equals( "ok" )){ c.cancel(); t.cancel(); Toast.makeText(MainActivity. this ,r,Toast.LENGTH_SHORT).show(); r= "" ; } else if (r.equals( "" )){ c.cancel(); t.cancel(); Toast.makeText(MainActivity. this ,r,Toast.LENGTH_SHORT).show(); r= "no user" ; } else if (r.equals( "error password" )){ c.cancel(); t.cancel(); Toast.makeText(MainActivity. this ,r,Toast.LENGTH_SHORT).show(); r= "" ; } } }); } }, 1 , 1000 ); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true ; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true ; } return super .onOptionsItemSelected(item); } } |
کد کلاس :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package com.login.login; import android.os.AsyncTask; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; public class clas_login extends AsyncTask { private String Link; private String User; private String Password; public clas_login(String link, String user, String password) { Link=link; User=user; Password=password; } @Override protected Object doInBackground(Object... arg0) { try { String data=URLEncoder.encode( "user" , "UTF-8" )+ "=" +URLEncoder.encode(User, "UTF-8" ); data+= "&" + URLEncoder.encode( "password" , "UTF-8" )+ "=" +URLEncoder.encode(Password, "UTF-8" ); URL con = new URL(Link); URLConnection connect=con.openConnection(); connect.setDoOutput( true ); OutputStreamWriter wr= new OutputStreamWriter(connect.getOutputStream()); wr.write(data); wr.close(); BufferedReader read= new BufferedReader( new InputStreamReader(connect.getInputStream())); StringBuilder sb= new StringBuilder(); String line= null ; while ((line=read.readLine())!= null ){ sb.append(line); } MainActivity.r=sb.toString(); } catch (Exception e){ } return null ; } } |