1

我使用 USB 调试测试了应用程序和设备,但得到了不同的结果。在模拟器中它工作正常,但在设备上我无法登录,只是扔吐司

Toast.makeText(Login.this,"result is null- an error occured",Toast.LENGTH_SHORT).show();. 

为什么会这样?我的模拟器在 OS 2.3.3 上运行,我的设备在 4.0 上运行。

当我在我的设备上测试时,logcat 没有出现,logcat 仅在我在模拟器 Oo 上运行时显示进程

任何人请帮助我,非常感谢。

登录.java

public class Login extends Activity {
    public Koneksi linkurl;
    String SERVER_URL;
    private Button login, register, setting;
    private EditText username, password;
    public ProgressDialog progressDialog;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        setting = (Button)findViewById(R.id.bsetting);
        login = (Button) findViewById(R.id.login);
        register = (Button) findViewById(R.id.reg);
        username = (EditText) findViewById(R.id.uname);
        password = (EditText) findViewById(R.id.pass);

        setting.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intentSet = new Intent(Login.this, UrlSetting.class);
                startActivity(intentSet);
            }
        });

        register.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intentReg = new Intent(Login.this, Register.class);
                startActivity(intentReg);
            }
        });

        login.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                // TODO Auto-generated method stub

               new LoginTask().execute();



            }
        });


    }
    protected String tryLogin(String mUsername, String mPassword)
    {           
      Log.d(" TryLoginCheck ","Here");
        HttpURLConnection connection;
       OutputStreamWriter request = null;

            URL url = null;
            String response = null;   
            String temp=null;
            String parameters = "username="+mUsername+"&password="+mPassword;   
            System.out.println("UserName"+mUsername+"\n"+"password"+mPassword);
            Log.d("Parameters",parameters);
            try
            {
                ;
                linkurl = new Koneksi(this);
                SERVER_URL = linkurl.getUrl();
                SERVER_URL += "/mobile/Login.php";
                url = new URL(SERVER_URL);
                connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setRequestMethod("POST");    

                request = new OutputStreamWriter(connection.getOutputStream());
                request.write(parameters);
                request.flush();
                request.close();            
                String line = "";               
                InputStreamReader isr = new InputStreamReader(connection.getInputStream());
                BufferedReader reader = new BufferedReader(isr);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null)
                {

                    sb.append(line + "\n");
                }
                temp=sb.toString();
                Log.d("Temp",temp);

                response = sb.toString();
                Log.d("Response",response);
               Log.d("Sb Value",sb.toString());
                isr.close();
                reader.close();


            }
            catch(IOException e)
            {

                return null;
            }

            return response;
    }

    public class LoginTask extends AsyncTask<String, String, String> {

        String result = null;

        @Override
        protected void onPreExecute()
        {

        }
        @Override
        protected String doInBackground(String... arg0)
        {
            String mUsername = username.getText().toString();
               String mPassword = password.getText().toString();          
               return tryLogin(mUsername, mPassword);
        }
      protected void onPostExecute(String result){
          super.onPostExecute(result);
          if(result==null)
          {
              Toast.makeText(Login.this,"result is null- an error occured",Toast.LENGTH_SHORT).show();
            } 
          else{

              result = result.trim();
         Log.d("Check","Here");
            Log.d("Response",result);
         if(result.toLowerCase().contains("berhasil"))
                {
                    String nama = username.getText().toString();
                    Intent newIntent = new Intent(Login.this, MainPage.class);

                    Bundle bundle = new Bundle();

                    bundle.putString("nama", nama);

                    newIntent.putExtras(bundle);
                    startActivityForResult(newIntent, 0);
                }
                else
                {
                    //Optional
                    //Kalau bisa dibuat constant untuk menghindari salah penulisan
                    String RoleError = "ROLE SALAH";
                    String UserError = "USER SALAH";

                    createDialog("Maaf", result.equals(RoleError) ? "Role Anda bukan Student!" : "Username Atau Password Salah!");
                }
          }
      }
    }
    private void createDialog(String title, String text) {
        AlertDialog ad = new AlertDialog.Builder(this)
        .setPositiveButton("Ok", null)
        .setTitle(title)
        .setMessage(text)
        .create();
        ad.show();
    }
}
4

3 回答 3

1

在这里看到你正在做的UI related操作doInBacground()。我们不能那样做,只是把代码放进去preExecute(),然后在你想要的地方使用这些值。像这样

String result = null;
String mUsername = null;
String mPassword = null;

@Override
protected void onPreExecute()
{
    mUsername = username.getText().toString();
    mPassword = password.getText().toString();  
}

@Override
protected String doInBackground(String... arg0)
{       
    return tryLogin(mUsername, mPassword);
}
于 2013-06-10T12:03:26.850 回答
0

我认为这个问题会解决你的问题,我以前也有过:HttpURLConnection failed on Android 4.0 All is related to httpurlconnection and internet policy。

于 2013-06-10T11:53:23.633 回答
0

您可以使用此代码(将其放在代码中某处的顶部):

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

但严格检查/测试您的 AsyncTask 是否良好。
如果您的代码在此之后工作,则 AsyncTask 不好。
如果它仍然不起作用,则有其他问题。

但仅使用此代码检查它,不要在最终程序中使用它。

于 2013-06-10T12:07:01.943 回答