英文:
Android application is not working properly in mobile but working in emulator
问题
我想在Android中设计一个登录页面,通过PHP访问MySQL数据库,
应用程序在模拟器上运行正常,但在手机上却没有,甚至第二次单击按钮时都没有进入backgroung.java。我认为是因为在logcat中没有打印出输入的消息,如果有任何帮助,我会非常感激,因为我刚接触Android。
主活动(Main Activity)
public void clicklogin(View view) {
//validate(email.getText().toString(),pass.getText().toString());
String username = email.getText().toString();
String password = pass.getText().toString();
String type = "login";
BackgroungWorker backgroundWorker = new BackgroungWorker(this);
backgroundWorker.execute(type, username, password);
Log.i("check msg","clicked");
}
后台工作者(BackgroundWorker)
public class BackgroungWorker extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
BackgroungWorker(Context ctx) {
context = ctx;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected String doInBackground(String... params) {
Log.i("check msg", "entered");
String type = params[0];
String login_url = "http://192.168.0.110/login.php";
if (type.equals("login")) {
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&"
+ URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.ISO_8859_1));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
Log.i("check msg", "hello " + result);
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result + "hi");
alertDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
2020-04-06 02:49:49.622 24596-24620/? I/OpenGLRenderer: Initialized EGL, version 1.4
2020-04-06 02:49:49.622 24596-24620/? D/OpenGLRenderer: Swap behavior 1
2020-04-06 02:49:49.635 24596-24596/? I/ViewConfigCompat: Could not find method getScaledScrollFactor() on ViewConfiguration
2020-04-06 02:49:49.699 24596-24608/? D/ActivityThread: scheduleCPUPowerControlForApp pkgName:com.example.myapplication using hwrender:true
2020-04-06 02:49:57.404 24596-24596/com.example.myapplication I/check msg: clicked
2020-04-06 02:49:57.405 24596-24638/com.example.myapplication I/check msg: entered
2020-04-06 02:49:57.407 24596-24638/com.example.myapplication D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-04-06 02:50:01.667 24596-24596/com.example.myapplication I/check msg: clicked
2020-04-06 02:50:02.882 24596-24596/com.example.myapplication I/check msg: clicked
2020-04-06 02:50:09.626 24596-24596/com.example.myapplication I/check msg: clicked
2020-04-06 02:50:09.838 24596-24596/com.example.myapplication I/check msg: clicked
2020-04-06 02:50:10.253 24596-24596/com.example.myapplication I/check msg: clicked
英文:
I want to design a login page in android accessing a MySQL database using PHP,
Application is working fine on emulator but not on mobile it is not even entering in backgroung.java second time I click the button I think as it is not printing entered message in logcat any help will be appreciated as I am new in android.
Main Activity
public void clicklogin(View view) {
//validate(email.getText().toString(),pass.getText().toString());
String username = email.getText().toString();
String password = pass.getText().toString();
String type = "login";
BackgroungWorker backgroundWorker = new BackgroungWorker(this);
backgroundWorker.execute(type, username, password);
Log.i("check msg","clicked");
}
BackgroungWorker
public class BackgroungWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
BackgroungWorker(Context ctx)
{
context=ctx;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected String doInBackground(String... params) {
Log.i("check msg","entered");
String type = params[0];
String login_url = "http://192.168.0.110/login.php";
if(type.equals("login")) {
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.ISO_8859_1));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
Log.i("check msg","hello "+result);
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog=new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result+"hi");
alertDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
2020-04-06 02:49:49.622 24596-24620/? I/OpenGLRenderer: Initialized EGL, version 1.4
2020-04-06 02:49:49.622 24596-24620/? D/OpenGLRenderer: Swap behavior 1
2020-04-06 02:49:49.635 24596-24596/? I/ViewConfigCompat: Could not find method getScaledScrollFactor() on ViewConfiguration
2020-04-06 02:49:49.699 24596-24608/? D/ActivityThread: scheduleCPUPowerControlForApp pkgName:com.example.myapplication using hwrender:true
2020-04-06 02:49:57.404 24596-24596/com.example.myapplication I/check msg: clicked
2020-04-06 02:49:57.405 24596-24638/com.example.myapplication I/check msg: entered
2020-04-06 02:49:57.407 24596-24638/com.example.myapplication D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-04-06 02:50:01.667 24596-24596/com.example.myapplication I/check msg: clicked
2020-04-06 02:50:02.882 24596-24596/com.example.myapplication I/check msg: clicked
2020-04-06 02:50:09.626 24596-24596/com.example.myapplication I/check msg: clicked
2020-04-06 02:50:09.838 24596-24596/com.example.myapplication I/check msg: clicked
2020-04-06 02:50:10.253 24596-24596/com.example.myapplication I/check msg: clicked
专注分享java语言的经验与见解,让所有开发者获益!
评论