0

源代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initialize();
}
    public void initialize() {
    tvValid = (TextView) findViewById(R.id.tvLoginValidity);
    bSubmit = (Button) findViewById(R.id.bLoginSubmit);
    bSubmit.setOnClickListener(this);
    name = (EditText) findViewById(R.id.etLoginName);
    pass = (EditText) findViewById(R.id.etLoginPass);
    //dbManager = new DatabaseManager("http://10.0.2.2:80/dbProject/login.php");
    //dbManager = new DatabaseManager("http://203.162.10.109/italk/ittalk/login.jsp");
    dbManager = new DatabaseManager("http://10.171.33.8/italk/ittalk/login.jsp");
}

@Override
public void onClick(View v) {
    if(name ==null || pass == null)
        return;
    else{
        if(dbManager.checkPassword(name.getText().toString(), pass.getText().toString())){
            tvValid.setText("Correct!!");
            Intent i  = new Intent("android.intent.action.MENU");
            startActivity(i);
        }else{
            tvValid.setText("Invalid Username or Password");

        }
    }
}

//public boolean checkPassword(String name, String pwd) {

    String password;
    try {

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("username", name));
        nameValuePairs.add(new BasicNameValuePair("db_name", "jdbc/ITTALKDS"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        String jsonResult = inputStreamToString(
                response.getEntity().getContent()).toString();
        if (jsonResult.equals(null))
            return false;
        JSONTokener tokener = new JSONTokener(jsonResult.toString());
        JSONArray jArray = new JSONArray(tokener);

        JSONObject json_data;
        for (int i = 0; i < jArray.length(); i++) {
            json_data = jArray.getJSONObject(i);
            password = json_data.getString("PASS_WORD");
            if (password.equals(pwd)) {
                return true;
            } else
                return false;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

错误

06-27 15:43:07.912: E/AndroidRuntime(535): FATAL EXCEPTION: main
06-27 15:43:07.912: E/AndroidRuntime(535): android.os.NetworkOnMainThreadException
06-27 15:43:07.912: E/AndroidRuntime(535):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
06-27 15:43:07.912: E/AndroidRuntime(535):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:74)
06-27 15:43:07.912: E/AndroidRuntime(535):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-27 15:43:07.912: E/AndroidRuntime(535):  at libcore.io.IoBridge.connect(IoBridge.java:112)
06-27 15:43:07.912: E/AndroidRuntime(535):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-27 15:43:07.912: E/AndroidRuntime(535):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
06-27 15:43:07.912: E/AndroidRuntime(535):  at java.net.Socket.connect(Socket.java:842)
06-27 15:43:07.912: E/AndroidRuntime(535):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
06-27 15:43:07.912: E/AndroidRuntime(535):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
06-27 15:43:07.912: E/AndroidRuntime(535):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-27 15:43:07.912: E/AndroidRuntime(535):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-27 15:43:07.912: E/AndroidRuntime(535):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-27 15:43:07.912: E/AndroidRuntime(535):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-27 15:43:07.912: E/AndroidRuntime(535):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-27 15:43:07.912: E/AndroidRuntime(535):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-27 15:43:07.912: E/AndroidRuntime(535):  at lk.sachith.databasesystem.DatabaseManager.checkPassword(DatabaseManager.java:51)
06-27 15:43:07.912: E/AndroidRuntime(535):  at lk.sachith.databasesystem.MainActivity.onClick(MainActivity.java:48)
06-27 15:43:07.912: E/AndroidRuntime(535):  at android.view.View.performClick(View.java:3480)
06-27 15:43:07.912: E/AndroidRuntime(535):  at android.view.View$PerformClick.run(View.java:13983)
06-27 15:43:07.912: E/AndroidRuntime(535):  at android.os.Handler.handleCallback(Handler.java:605)
06-27 15:43:07.912: E/AndroidRuntime(535):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-27 15:43:07.912: E/AndroidRuntime(535):  at android.os.Looper.loop(Looper.java:137)
06-27 15:43:07.912: E/AndroidRuntime(535):  at android.app.ActivityThread.main(ActivityThread.java:4340)
06-27 15:43:07.912: E/AndroidRuntime(535):  at java.lang.reflect.Method.invokeNative(Native Method)
06-27 15:43:07.912: E/AndroidRuntime(535):  at java.lang.reflect.Method.invoke(Method.java:511)
06-27 15:43:07.912: E/AndroidRuntime(535):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-27 15:43:07.912: E/AndroidRuntime(535):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-27 15:43:07.912: E/AndroidRuntime(535):  at dalvik.system.NativeStart.main(Native Method)
06-27 15:43:10.141: I/Process(535): Sending signal. PID: 535 SIG: 9
4

1 回答 1

0

这个错误很常见,问题是你试图在 UI 线程中访问互联网,这在 Android 4.0+ 中不再可能。将您的checkPassword代码放在AsyncTask中

于 2013-06-27T09:05:05.203 回答