Android应用无法正常连接到互联网,并且在尝试登录时遇到困难

时间:2015-08-29 23:02:42

标签: android phpmyadmin

我正在开发一个Android应用程序,我使用localhost它工作正常,但在网上它不工作。它在连接到wampserver时工作正常但是当我尝试使它在线工作时它不会连接并且总是卡在尝试登录时不知道为什么会发生这种情况并且我的PHP代码也能正常工作。 / p>

private static final String LOGIN_URL = "http://omnibuscaps.esy.es/capstone/login.php";

private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";

public final static int WHITE = 0xFFFFFFFF;
public final static int BLACK = 0xFF000000;
public final static int WIDTH = 400;
public final static int HEIGHT = 400;
public final static String STR = "";


@Override
protected void onCreate(Bundle savedInstanceState) {
    //remove title
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    user = (EditText)findViewById(R.id.username);
    pass = (EditText)findViewById(R.id.accname2);
    bLogin = (Button)findViewById(R.id.login);
    bLogin.setOnClickListener(this);



}//end onCreate Method

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
     switch (v.getId()) {//start switch
     case R.id.login:
                new AttemptLogin().execute();

     // here we have used, switch case, because on login activity you may //also want to show registration button, so if the user is new ! we can go the //registration activity , other than this we could also do this without switch //case.
     default:
           break;
     }//end switch
}

class AttemptLogin extends AsyncTask<String, String, String>{

    @Override
    protected void onPreExecute(){
        super.onPreExecute();

        pDialog = new ProgressDialog(Login.this);
        pDialog.setMessage("Attempting for login...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
          // here Check for success tag
        int success;
        String username = user.getText().toString();
        String password = pass.getText().toString();
        try {

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", username));
            params.add(new BasicNameValuePair("password", password));

            Log.d("request!", "starting");

            JSONObject json = jsonparser.makeHttpRequest(
                   LOGIN_URL, "POST", params);

             //checking  log for json response
            Log.d("Login attempt", json.toString());

            // success tag for json
            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
              Log.d("Successfully Login!", json.toString());

              parseuser = user.getText().toString();//get username text
              parsepass = pass.getText().toString();//get password text
             Intent ii = new Intent(Login.this, Client.class);//initialize next activitty
             //Intent ii = new Intent(Login.this, NavActivity.class);
              ii.putExtra("usertext", parseuser);//store username
             ii.putExtra("passtext", parsepass);//store password
              finish();
             //this finish() //method is used to tell android os that we are done with current //activity now! Moving to other activity
             startActivity(ii);//start next activity

              return json.getString(TAG_MESSAGE);
            }else{
              pDialog.setMessage("Wrong username or password");
              pDialog.setCancelable(true);
              pDialog.show();
              return json.getString(TAG_MESSAGE);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

}

protected void onPostExecute(String message){
    pDialog.dismiss();
    if(message != null){
        Toast.makeText(Login.this, "OK na", Toast.LENGTH_LONG).show();      
    }
}

我的日志猫内容

08-29 19:08:31.477: W/System.err(2353): java.lang.NullPointerException
08-29 19:08:31.477: W/System.err(2353):     at com.example.omniclient.Login$AttemptLogin.doInBackground(Login.java:111)
08-29 19:08:31.477: W/System.err(2353):     at com.example.omniclient.Login$AttemptLogin.doInBackground(Login.java:1)
08-29 19:08:31.477: W/System.err(2353):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-29 19:08:31.477: W/System.err(2353):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-29 19:08:31.477: W/System.err(2353):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-29 19:08:31.477: W/System.err(2353):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-29 19:08:31.477: W/System.err(2353):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-29 19:08:31.477: W/System.err(2353):     at java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:0)

您是否找到了问题的原因?正如我所评论的那样,您应该检查jsonjsonparser是否为空。看看

JSONObject json = jsonparser.makeHttpRequest

此外,请发布您的makeHttpRequest将帮助我们尽快找到解决方案。

希望这有帮助!