Android应用在手机上无法正常运行,但在模拟器中可以运行。

huangapple 未分类评论50阅读模式
英文:

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&#160;msg: clicked
2020-04-06 02:49:57.405 24596-24638/com.example.myapplication I/check&#160;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&#160;msg: clicked
2020-04-06 02:50:02.882 24596-24596/com.example.myapplication I/check&#160;msg: clicked
2020-04-06 02:50:09.626 24596-24596/com.example.myapplication I/check&#160;msg: clicked
2020-04-06 02:50:09.838 24596-24596/com.example.myapplication I/check&#160;msg: clicked
2020-04-06 02:50:10.253 24596-24596/com.example.myapplication I/check&#160;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 = &quot;login&quot;;
    BackgroungWorker backgroundWorker = new BackgroungWorker(this);
    backgroundWorker.execute(type, username, password);
    Log.i(&quot;check msg&quot;,&quot;clicked&quot;);


}

BackgroungWorker

public class BackgroungWorker extends AsyncTask&lt;String,Void,String&gt; {
Context context;
AlertDialog alertDialog;

BackgroungWorker(Context ctx)
{
    context=ctx;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected String doInBackground(String... params) {
    Log.i(&quot;check msg&quot;,&quot;entered&quot;);
    String type = params[0];
    String login_url = &quot;http://192.168.0.110/login.php&quot;;

    if(type.equals(&quot;login&quot;)) {
        try {
            String user_name = params[1];
            String password = params[2];
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod(&quot;POST&quot;);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, &quot;UTF-8&quot;));
            String post_data = URLEncoder.encode(&quot;user_name&quot;,&quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(user_name,&quot;UTF-8&quot;)+&quot;&amp;&quot;
                    +URLEncoder.encode(&quot;password&quot;,&quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(password,&quot;UTF-8&quot;);
            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=&quot;&quot;;
            String line=&quot;&quot;;
            while((line = bufferedReader.readLine())!= null) {
                result += line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            Log.i(&quot;check msg&quot;,&quot;hello &quot;+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(&quot;Login Status&quot;);
}

@Override
protected void onPostExecute(String result) {
    alertDialog.setMessage(result+&quot;hi&quot;);
    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&#160;msg: clicked
2020-04-06 02:49:57.405 24596-24638/com.example.myapplication I/check&#160;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&#160;msg: clicked
2020-04-06 02:50:02.882 24596-24596/com.example.myapplication I/check&#160;msg: clicked
2020-04-06 02:50:09.626 24596-24596/com.example.myapplication I/check&#160;msg: clicked
2020-04-06 02:50:09.838 24596-24596/com.example.myapplication I/check&#160;msg: clicked
2020-04-06 02:50:10.253 24596-24596/com.example.myapplication I/check&#160;msg: clicked

huangapple
  • 本文由 发表于 2020年4月6日 05:46:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/61049969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定