当我想在我的活动中注册时,我收到了这个错误:
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 任务)。但我不知道具体如何。你能帮我吗 ?