android.os.NetworkOnMainThreadException



我在我的代码中使用了asynctask。但是它显示了我在日志中给出的错误。我已经创建了api 18的谷歌api模拟器。因为它不起作用。但是在api8中工作。是否可以在任何版本的实际设备上运行?

btnLogin.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                new LoginTask_1().execute();
            }
        });

asynctask

class LoginTask_1 extends AsyncTask<String, String, String> {
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(RegisterActivity.this);
            pDialog.setMessage("loging in...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        /**
         * Deleting product
         * */
        protected String doInBackground(String... args) {
            runOnUiThread(new Runnable() {
                public void run() {
                    String code = inputcode.getText().toString();
                    UserFunctions userFunction = new UserFunctions();
                    Log.d("Button", "Login");
                    JSONObject json = userFunction.loginUser1(id,code);
                    // check for login response
                    try {
                        if (json.getString(KEY_SUCCESS) != null) {
                            loginErrorMsg1.setText("");
                            String res = json.getString(KEY_SUCCESS); 
                            if(Integer.parseInt(res) == 1){
                                DatabaseHandler1 db = new DatabaseHandler1(getApplicationContext());
                                db.addUser(adduser1, adduser2,id, adduser3,adduser4);
                                // Launch Dashboard Screen
                                Intent dashboard = new Intent(getApplicationContext(), MainActivity.class);
                                dashboard.putExtra("name", id);
                                dashboard.putExtra("email", adduser4);
                                //dashboard.putExtra(EXTRA_MESSAGE, id);
                                // Close all views before launching Dashboard
                                dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(dashboard);
                                // Close Login Screen
                                finish();
                                }
                            /*else if(Integer.parseInt(res) == 2){
                                loginErrorMsg.setText("Already logged in");
                            }*/
                            else{
                                // Error in login
                                loginErrorMsg1.setText("Invalid Code");
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
            return null;
        }
        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once product deleted
            pDialog.dismiss();
        }
    }

logcat

03-21 06:47:54.403: E/AndroidRuntime(3265): FATAL EXCEPTION: main
03-21 06:47:54.403: E/AndroidRuntime(3265): android.os.NetworkOnMainThreadException
03-21 06:47:54.403: E/AndroidRuntime(3265):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at com.androidhive.pushnotifications.JSONParser.getJSONFromUrl(JSONParser.java:47)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at com.androidhive.pushnotifications.UserFunctions.loginUser(UserFunctions.java:46)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at com.androidhive.pushnotifications.LoginActivity$LoginTask$1.run(LoginActivity.java:125)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at android.os.Handler.handleCallback(Handler.java:730)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at android.os.Looper.loop(Looper.java:137)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at java.lang.reflect.Method.invokeNative(Native Method)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at java.lang.reflect.Method.invoke(Method.java:525)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-21 06:47:54.403: E/AndroidRuntime(3265):     at dalvik.system.NativeStart.main(Native Method)

删除runOnUiThread

runOnUiThread(new Runnable() {
            public void run() {

你的doInbackground在后台线程上被调用。你不需要runOnUiThread来做网络操作。

doInbackground返回结果,在onPostExecute返回startActivity。

编辑:

根据prosper的建议,在onPostExecute中更新ui,而不是在doInbackground中更新ui

  loginErrorMsg1.setText("Invalid Code");

你也可以从EditText在活动和传递字符串到doInbackground的文本

   btnLogin.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            String code = inputcode.getText().toString();
            new LoginTask_1().execute(code);
        }
    });
然后

   protected String doInBackground(String... args) {
   String value = args[0];  

相关内容

  • 没有找到相关文章

最新更新