0

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

但是我有这样的情况;

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

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

但我是个例外。请帮我解决这个我更新的代码

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();

    }
}



}
4

1 回答 1

1

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

AsyncTask会为您完成大部分的簿记工作,让这一切变得更容易一些。有关详细信息,请参阅博文Painless Threading

于 2012-09-09T19:16:11.027 回答