-1

我使用 Eclipse 开发应用程序,我使用 Android 2.1 API 7,我的应用程序与外部数据库 (JSON) 交互。当我的模拟器使用 Android 2.1 时我没有任何问题,但是当我使用 Android 4.0 开启我的模拟器时,当我与外部数据库交互时我的应用程序崩溃......

Logcat 日志:

11-18 18:44:56.852: D/dalvikvm(601): Not late-enabling CheckJNI (already on)
11-18 18:44:58.141: D/gralloc_goldfish(601): Emulator without GPU emulation detected.
11-18 18:44:58.301: W/TextLayoutCache(601): computeValuesWithHarfbuzz -- need to force to single run
11-18 18:44:58.301: W/TextLayoutCache(601): computeValuesWithHarfbuzz -- need to force to single run
11-18 18:45:06.601: D/AndroidRuntime(601): Shutting down VM
11-18 18:45:06.601: W/dalvikvm(601): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
11-18 18:45:06.631: E/AndroidRuntime(601): FATAL EXCEPTION: main
11-18 18:45:06.631: E/AndroidRuntime(601): android.os.NetworkOnMainThreadException
11-18 18:45:06.631: E/AndroidRuntime(601):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
11-18 18:45:06.631: E/AndroidRuntime(601):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
11-18 18:45:06.631: E/AndroidRuntime(601):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
11-18 18:45:06.631: E/AndroidRuntime(601):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
11-18 18:45:06.631: E/AndroidRuntime(601):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-18 18:45:06.631: E/AndroidRuntime(601):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-18 18:45:06.631: E/AndroidRuntime(601):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-18 18:45:06.631: E/AndroidRuntime(601):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-18 18:45:06.631: E/AndroidRuntime(601):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-18 18:45:06.631: E/AndroidRuntime(601):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-18 18:45:06.631: E/AndroidRuntime(601):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-18 18:45:06.631: E/AndroidRuntime(601):  at com.example.library.JSONParser.getJSONFromUrl(JSONParser.java:42)
11-18 18:45:06.631: E/AndroidRuntime(601):  at com.example.library.UserFunctions.loginUser(UserFunctions.java:32)
11-18 18:45:06.631: E/AndroidRuntime(601):  at com.example.lagrandechasse.LoginActivity$1.onClick(LoginActivity.java:54)
11-18 18:45:06.631: E/AndroidRuntime(601):  at android.view.View.performClick(View.java:3480)
11-18 18:45:06.631: E/AndroidRuntime(601):  at android.view.View$PerformClick.run(View.java:13983)
11-18 18:45:06.631: E/AndroidRuntime(601):  at android.os.Handler.handleCallback(Handler.java:605)
11-18 18:45:06.631: E/AndroidRuntime(601):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-18 18:45:06.631: E/AndroidRuntime(601):  at android.os.Looper.loop(Looper.java:137)
11-18 18:45:06.631: E/AndroidRuntime(601):  at android.app.ActivityThread.main(ActivityThread.java:4340)
11-18 18:45:06.631: E/AndroidRuntime(601):  at java.lang.reflect.Method.invokeNative(Native Method)
11-18 18:45:06.631: E/AndroidRuntime(601):  at java.lang.reflect.Method.invoke(Method.java:511)
11-18 18:45:06.631: E/AndroidRuntime(601):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-18 18:45:06.631: E/AndroidRuntime(601):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-18 18:45:06.631: E/AndroidRuntime(601):  at dalvik.system.NativeStart.main(Native Method)
11-18 18:45:08.711: I/Process(601): Sending signal. PID: 601 SIG: 9

你能帮助我吗 ?

感谢您的回复,这是我的登录课程:

public class LoginActivity extends Activity {

    //My variable

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        //initialize button and textwiew

        btnLogin.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                UserFunctions userFunction = new UserFunctions();
                JSONObject json = userFunction.loginUser(login,pass);

                try {
                    if (json.getString(success) != null) {
                        loginErrorMsg.setText("");
                        String res = json.getString(success);
                        if(Integer.parseInt(res) == 1){

                            DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                            JSONObject json_user = json.getJSONObject("user");

                            userFunction.logoutUser(getApplicationContext());
                            db.addUser(json_user.getString(ID), json_user.getString(login));                        

                            Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);

                            dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(dashboard);

                            finish();
                        }
                        else{
                            loginErrorMsg.setText("Error");
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

你能告诉我修改吗?谢谢 !

4

3 回答 3

0

你不能在主线程上进行网络操作,这就是日志跟踪告诉你的。

11-18 18:45:06.631: E/AndroidRuntime(601): FATAL EXCEPTION: main 11-18 18:45:06.631: E/AndroidRuntime(601): android.os.NetworkOnMainThreadException

始终在单独的线程上进行网络连接。您可以通过不同的方式来实现这一点,要么自己使用一个线程,要么asyncTask自己启动一个新线程。SO上有很多关于这个的帖子,网上有很多例子。看看这个 SO 帖子

于 2012-11-18T19:10:25.683 回答
0

您正在主 UI 线程中执行(可能很慢)网络操作。如果您的目标 SDK 是 11 (Honeycomb) 或更高版本,则会抛出NetworkOnMainThreadException,因为此行为可能会阻塞 UI 并导致应用程序无响应。

您可以使用 anAsyncTask来解决这个问题,方法是将数据加载到doInBackground(..).

于 2012-11-18T18:55:04.343 回答
0

用这种方式。。

public class getConnection extends AsyncTask<String, Void, Void> 
    {


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

           }

           @Override
           protected Void doInBackground(String... arg0)
           {

            try 
            {
                 //Your JSON Code  
            } 
            catch (Exception e) 
            {
             e.printStackTrace();
            }

            return null;
           }

           @Override
           protected void onPostExecute(Void result)
           {
               super.onPostExecute(result);


            }
            catch (Exception e)
            {
             Log.v("log", e.toString());
            }
           }

          }
于 2012-11-18T19:00:33.497 回答