0

当我想在我的活动中注册时,我收到了这个错误:

E/AndroidRuntime(1797): FATAL EXCEPTION: main
E/AndroidRuntime(1797): android.os.NetworkOnMainThreadException
E/AndroidRuntime(1797):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
E/AndroidRuntime(1797):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
E/AndroidRuntime(1797):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
E/AndroidRuntime(1797):     at libcore.io.IoBridge.connect(IoBridge.java:112)
E/AndroidRuntime(1797):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
E/AndroidRuntime(1797):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
E/AndroidRuntime(1797):     at java.net.Socket.connect(Socket.java:842)
E/AndroidRuntime(1797):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
E/AndroidRuntime(1797):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
E/AndroidRuntime(1797):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
E/AndroidRuntime(1797):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
E/AndroidRuntime(1797):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
E/AndroidRuntime(1797):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
E/AndroidRuntime(1797):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
E/AndroidRuntime(1797):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
E/AndroidRuntime(1797):     at com.anypli.maboitepharmacie.utiles.JSONParser.getJSONFromUrl(JSONParser.java:39)
E/AndroidRuntime(1797):     at com.anypli.maboitepharmacie.ws.PatientFunctions.registerUser(PatientFunctions.java:76)
E/AndroidRuntime(1797):     at com.anypli.maboitepharmacie.activitiy.RegisterPatient$2.onClick(RegisterPatient.java:99)
E/AndroidRuntime(1797):     at android.view.View.performClick(View.java:4202)
E/AndroidRuntime(1797):     at android.view.View$PerformClick.run(View.java:17340)
E/AndroidRuntime(1797):     at android.os.Handler.handleCallback(Handler.java:725)
E/AndroidRuntime(1797):     at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(1797):     at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(1797):     at android.app.ActivityThread.main(ActivityThread.java:5039)
E/AndroidRuntime(1797):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1797):     at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(1797):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(1797):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(1797):     at dalvik.system.NativeStart.main(Native Method)

我实际上做了一个网络服务来进行注册。这是我的注册活动

public class RegisterPatient extends Activity {

EditText inputName;
EditText inputEmail;
EditText inputPassword;

Button buttonRegister;

private static String KEY_SUCCESS = "success"; 

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

    inputName = (EditText) findViewById(R.id.registerName);
    inputEmail = (EditText) findViewById(R.id.registerEmail);
    inputPassword = (EditText) findViewById(R.id.registerPassword);
    buttonRegister = (Button) findViewById(R.id.btnRegister);

    // Register Button Click event
    buttonRegister.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            String name = inputName.getText().toString();
            String mail = inputEmail.getText().toString();
            String password = inputPassword.getText().toString();

            PatientFunctions patientFunction = new PatientFunctions();
            JSONObject json;

            json = patientFunction.registerUser(name, mail,
                    password);

            try {
                if (json.getString(KEY_SUCCESS) != null) {

                    String res = json.getString(KEY_SUCCESS);
                    if (Integer.parseInt(res) == 1) {
                        // user successfully registred
                               Toast.makeText(getApplicationContext(),"Patient Enregistré", Toast.LENGTH_SHORT).show();

                    } else {
                        Toast.makeText(getApplicationContext(), "Erreur",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    });
}
}

我认为我应该使用线程来解决这个问题(而不是 Assync 任务)。但我不知道具体如何。你能帮我吗 ?

4

3 回答 3

1
// try this
buttonRegister.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                final String name = inputName.getText().toString();
                final String mail = inputEmail.getText().toString();
                final String password = inputPassword.getText().toString();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        PatientFunctions patientFunction = new PatientFunctions();
                        JSONObject json;
                        json = patientFunction.registerUser(name, mail,
                                password);

                        try {
                            if (json.getString(KEY_SUCCESS) != null) {

                                String res = json.getString(KEY_SUCCESS);
                                if (Integer.parseInt(res) == 1) {
                                    // user successfully registred
                                    Toast.makeText(getApplicationContext(),"Patient Enregistré", Toast.LENGTH_SHORT).show();

                                } else {
                                    Toast.makeText(getApplicationContext(), "Erreur",
                                            Toast.LENGTH_SHORT).show();
                                }
                            }
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });


            }
        });
于 2013-10-04T09:58:25.493 回答
0

这是您不使用 asynctask 的解决方案。但我强烈建议使用 asynctask ..这将适合您的目的。从 android 2.3 开始,网络或长时间运行的任务不允许在主 UI 线程上运行

Thread thread = new Thread()
{
    // do network stuff here 
};

thread.start();
于 2013-10-04T09:59:41.440 回答
0

android 不允许在 UI/主线程上进行 NETWORK 调用。只需通过将 Runnable 传递给它来创建一个新线程。

Thread t = new Thread(new Runnable() {

    @Override
    public void run() {
        // Code for registration should go here
    }
});

t.start();

使用AsyncTask(强烈推荐)会让你的生活变得轻松。

于 2013-10-04T10:06:26.027 回答