所以我使用Eclipse,PHP和MySQL为应用程序创建了一个登录功能...它在多个手机上运行正常(Xperia Play,Droidx,HTC Thunderbolt),但是,我无法让它工作我试过的任何平板电脑。 (Nexus 7,ASUS Transformer)。当我点击注册新用户时,我不断收到强制关闭错误。
决议有问题吗?或者它与我的代码有什么关系?
我使用了本教程:
http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/
谢谢!
编辑:这是我的日志:
12-01 18:22:14.896: D/AndroidRuntime(10040): Shutting down VM
12-01 18:22:14.896: W/dalvikvm(10040): threadid=1: thread exiting with uncaught exception (group=0x416f2930)
12-01 18:22:14.946: E/AndroidRuntime(10040): FATAL EXCEPTION: main
12-01 18:22:14.946: E/AndroidRuntime(10040): android.os.NetworkOnMainThreadException
12-01 18:22:14.946: E/AndroidRuntime(10040): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
12-01 18:22:14.946: E/AndroidRuntime(10040): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
12-01 18:22:14.946: E/AndroidRuntime(10040): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
12-01 18:22:14.946: E/AndroidRuntime(10040): at libcore.io.IoBridge.connect(IoBridge.java:112)
12-01 18:22:14.946: E/AndroidRuntime(10040): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
12-01 18:22:14.946: E/AndroidRuntime(10040): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
12-01 18:22:14.946: E/AndroidRuntime(10040): at java.net.Socket.connect(Socket.java:842)
12-01 18:22:14.946: E/AndroidRuntime(10040): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
12-01 18:22:14.946: E/AndroidRuntime(10040): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
12-01 18:22:14.946: E/AndroidRuntime(10040): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-01 18:22:14.946: E/AndroidRuntime(10040): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-01 18:22:14.946: E/AndroidRuntime(10040): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
12-01 18:22:14.946: E/AndroidRuntime(10040): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-01 18:22:14.946: E/AndroidRuntime(10040): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-01 18:22:14.946: E/AndroidRuntime(10040): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-01 18:22:14.946: E/AndroidRuntime(10040): at com.groupone.the.health.effect.library.JSONParser.getJSONFromUrl(JSONParser.java:42)
12-01 18:22:14.946: E/AndroidRuntime(10040): at com.groupone.the.health.effect.library.UserFunctions.registerUser(UserFunctions.java:61)
12-01 18:22:14.946: E/AndroidRuntime(10040): at com.groupone.the.health.effect.RegisterActivity$1.onClick(RegisterActivity.java:54)
12-01 18:22:14.946: E/AndroidRuntime(10040): at android.view.View.performClick(View.java:4202)
12-01 18:22:14.946: E/AndroidRuntime(10040): at android.view.View$PerformClick.run(View.java:17340)
12-01 18:22:14.946: E/AndroidRuntime(10040): at android.os.Handler.handleCallback(Handler.java:725)
12-01 18:22:14.946: E/AndroidRuntime(10040): at android.os.Handler.dispatchMessage(Handler.java:92)
12-01 18:22:14.946: E/AndroidRuntime(10040): at android.os.Looper.loop(Looper.java:137)
12-01 18:22:14.946: E/AndroidRuntime(10040): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-01 18:22:14.946: E/AndroidRuntime(10040): at java.lang.reflect.Method.invokeNative(Native Method)
12-01 18:22:14.946: E/AndroidRuntime(10040): at java.lang.reflect.Method.invoke(Method.java:511)
12-01 18:22:14.946: E/AndroidRuntime(10040): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-01 18:22:14.946: E/AndroidRuntime(10040): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-01 18:22:14.946: E/AndroidRuntime(10040): at dalvik.system.NativeStart.main(Native Method)
编辑#2: 这是“.RegisterActivity”的代码:
import org.json.JSONException;
import org.json.JSONObject;
import com.example.androidhive.library.DatabaseHandler;
import com.example.androidhive.library.UserFunctions;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class RegisterActivity extends Activity {
Button btnRegister;
Button btnLinkToLogin;
EditText inputFullName;
EditText inputEmail;
EditText inputPassword;
TextView registerErrorMsg;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
// Importing all assets like buttons, text fields
inputFullName = (EditText) findViewById(R.id.registerName);
inputEmail = (EditText) findViewById(R.id.registerEmail);
inputPassword = (EditText) findViewById(R.id.registerPassword);
btnRegister = (Button) findViewById(R.id.btnRegister);
btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
registerErrorMsg = (TextView) findViewById(R.id.register_error);
// Register Button Click event
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = inputFullName.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.registerUser(name, email, password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
registerErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully registred
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Registration Screen
finish();
}else{
// Error in registration
registerErrorMsg.setText("Error occured in registration");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
// Link to Login Screen
btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(i);
// Close Registration View
finish();
}
});
}
}
答案 0 :(得分:2)
你得到NetworkOnMainThreadException
。这意味着您在主UI线程中执行繁重的网络操作。在低于11的API级别中,您可以执行此操作,但在Android HC +上不再允许这样做。您可以使用Asynctask来避免这种情况。
答案 1 :(得分:1)
您是从其他线程或异步任务发出http请求吗?因为平板电脑大于api级别10.您可能面临严格的模式问题。尝试在异步任务中发出http请求。
答案 2 :(得分:0)
你能在主线程中建立网络流量吗?早期版本的android允许你这样做,但从Honeycomb开始,所有网络访问必须在另一个线程中完成。你提到的平板电脑运行冰淇淋三明治,所以这是可能的。
很难解决您的问题。请留下stacktraces和源代码。你的描述很模糊。
答案 3 :(得分:0)
android.os.NetworkOnMainThreadException
表示您正在主线程上进行调用,它有一个非常简单的解决方案:
在com.groupone.the.health.effect.RegisterActivity$1.onClick(RegisterActivity.java:54)
使用AsynTask(http://developer.android.com/reference/android/os/AsyncTask.html)进行注册。您必须将UI代码与网络代码分开。