主线程上的NetworkConnection和验证



我被警告不要在主线程上使用网络操作。

但我有这样一个案例;

我需要检查该值是否存在于数据库中,

如果是,我将继续进行另一项活动。

但我要破例。请帮我解决这个问题我更新的代码

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.initial);


    if(!isNetworkAvailable())
    {
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.customlayoutnetconnection);
        Button dialogButton = (Button) dialog.findViewById(R.id.Ok);
        Button dialogButton2 = (Button) dialog.findViewById(R.id.OpenCon);

        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                finish();
            }});
        dialogButton2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                ConnectivityManager manager = (ConnectivityManager)getSystemService(InitialActivity.CONNECTIVITY_SERVICE);

                Boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();

                Boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
                if(is3g){
                    Toast.makeText(context, "3G", Toast.LENGTH_LONG);
                }else if(isWifi){
                    Toast.makeText(context, "wifi", Toast.LENGTH_LONG);
                }else{
                    Toast.makeText(context, "nothing", Toast.LENGTH_LONG);
                    startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
                }
            }});
        dialog.show();
    }

    lost = (Button) findViewById(R.id.imlost);
    lost.setOnClickListener(this);
    looking = (Button) findViewById(R.id.imlookingfor);
    looking.setOnClickListener(this);
    about = (Button) findViewById(R.id.about);
    about.setOnClickListener(this);
    howto = (Button) findViewById(R.id.howto);
    howto.setOnClickListener(this);

}


@Override
public void onClick(View v) {

    if(v==lost)
    {
        Intent i = new Intent(getApplicationContext(),IamLostActivity.class);

        startActivity(i);
    }
    else if(v==looking)
    {
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.customlayout);
        Button dialogButton = (Button) dialog.findViewById(R.id.Find);
        code  = (EditText) dialog.findViewById(R.id.codeToFind);
        code.requestFocus();
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if(code.getText().toString().contentEquals(""))
                {
                    final Dialog dialog2 = new Dialog(context);
                    dialog2.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog2.setContentView(R.layout.customlayoutinfo);
                    TextView tv = (TextView) dialog2.findViewById(R.id.customInfo);
                    tv.setText(context.getString(R.string.BlankField));
                    Button dialogButton2 = (Button) dialog2.findViewById(R.id.Ok);
                    dialogButton2.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog2.dismiss();
                            } }); 
                        dialog2.show();
                }

                else 
                {
                    findFriend = new Runnable() {
                        @Override
                        public void run() {
                            httpclnt = new DefaultHttpClient();
                            httppst = new HttpPost(url);
                            try {   
                                nameValuePairs = new ArrayList<NameValuePair>(1);
                                nameValuePairs.add((new BasicNameValuePair("code", code.getText().toString())));
                                httppst.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                                response = httpclnt.execute(httppst, responseHandler);
                                //message = response;
                            }
                            catch (ClientProtocolException e) {
                                Toast.makeText(InitialActivity.this, "Client protocol exception ", Toast.LENGTH_LONG).show();
                            } catch (IOException e) {
                                Toast.makeText(InitialActivity.this, "IO exception "+e.getMessage(), Toast.LENGTH_LONG).show();
                            }
                       }
                    };
                    Thread thread = new Thread(null, findFriend, "FindFriend");
                    thread.start();
                                if(response.contentEquals(","))
                                {

                                    final Dialog dialog4 = new Dialog(context);
                                    dialog4.requestWindowFeature(Window.FEATURE_NO_TITLE);
                                    dialog4.setContentView(R.layout.customlayoutinfo);
                                    TextView tv = (TextView) dialog4.findViewById(R.id.customInfo);
                                    tv.setText(context.getString(R.string.CheckCorrectness));
                                    Button dialogButton2 = (Button) dialog4.findViewById(R.id.Ok);
                                    dialogButton2.setOnClickListener(new OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            dialog4.dismiss();
                                            } }); 
                                        dialog4.show();
                                }
                                else
                                {
                                    Intent in = new Intent(getApplicationContext(),IamLookingForActivity.class);
                                    in.putExtra("message", response.toString());
                                    startActivity(in);
                                }
                    };

}

            }
        );

        Button dialogButton2 = (Button) dialog.findViewById(R.id.Cancel);
        dialogButton2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
      }
    else if(v==about)
    {
         final Dialog dialog = new Dialog(context);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.customlayoutabout);
            ImageButton dialogButton = (ImageButton) dialog.findViewById(R.id.kodatolye);
            Button dialogButton2 = (Button) dialog.findViewById(R.id.Back);
            dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    final Dialog dialog2 = new Dialog(context);
                    dialog2.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog2.setContentView(R.layout.customlayoutinfo);
                    TextView tv = (TextView) dialog2.findViewById(R.id.customInfo);
                    tv.setText("www.kodatolye.com");
                    Button dialogButton3 = (Button) dialog2.findViewById(R.id.Ok);

                    dialogButton3.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog2.dismiss();

                            } }); 
                    dialog2.show();
                    } }); 
            dialogButton2.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    } }); 
                dialog.show();
    }
    else if(v==howto)
    {

        final Dialog dialog2 = new Dialog(context);
        dialog2.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog2.setContentView(R.layout.customlayouthowto);

        Button dialogButton3 = (Button) dialog2.findViewById(R.id.Back);

        dialogButton3.setOnClickListener(new OnClickListener() {
            @Override
        public void onClick(View v) {
                dialog2.dismiss();
            } }); 
        dialog2.show();
    }
}

}

您不能在主线程上执行网络代码。同样,您不能尝试从主线程以外的任何更新UI。您(正确地)为网络操作启动了一个单独的线程,但您没有等待它完成。您应该将启动if(response.contentEquals(...))的代码移到Runnable中,直到网络活动完成并得到响应后,该代码才会运行。然后,您可以通过处理程序将Runnable发布到主线程,以启动新活动(或者根据需要显示错误消息)。您可能希望显示一个对话框,告诉用户在启动网络线程之前等待。

AsyncTask为您完成了大部分记账工作,使这一切变得更容易。有关更多信息,请参阅博客文章Painless Threading。

相关内容

  • 没有找到相关文章

最新更新