Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

multithreading - android os network on main thread exception

i make this code in single application working, but if join with other application , show this dialog "android os network on main thread exception"

 package com.kelompok2.bissmilahpulsa;
    import java.util.ArrayList;
    import org.apache.http.NameValuePair;
    import org.apache.http.message.BasicNameValuePair;
    import android.net.Uri;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RadioGroup;
    import android.widget.TextView;

    public class coba extends Activity {
      EditText nm,nmr;
      RadioGroup prov,nom,pem;
      TextView error;
      Button ok;

      @Override

  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.mulaitransaksi);

      nm=(EditText)findViewById(R.id.et_nama);
      nmr=(EditText)findViewById(R.id.et_nomor);
      prov=(RadioGroup)findViewById(R.id.provider);
      nom=(RadioGroup)findViewById(R.id.nominal);
      error=(TextView)findViewById(R.id.error);
      pem=(RadioGroup)findViewById(R.id.pembayaran);
      ok=(Button)findViewById(R.id.btn_ok);

      ok.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
              String type=null;
               String type1=null;
               String type2=null;
               switch (prov.getCheckedRadioButtonId()) 
               {
               case R.id.rb_as:
               type="As";
               break;
               case R.id.rb_axis:
               type="Axis";
               break;
               case R.id.rb_im3:
               type="Im3";
               break;
               case R.id.rb_telkomsel:
               type="Telokmsel";
               break;
               case R.id.rb_xl:
               type="Xl";
               break;
               }
               switch (nom.getCheckedRadioButtonId()) 
               {
               case R.id.rb_5:
               type1="5";
               break;
               case R.id.rb_10:
               type1="10";
               break;
               case R.id.rb_20:
               type1="20";
               break;
               case R.id.rb_50:
               type1="50";
               break;
               }
               switch (pem.getCheckedRadioButtonId()) 
               {
               case R.id.rb_cash:
               type2="Cash";
               break;
               case R.id.rb_hutang:
               type2="Hutang";
               break;
               }

              ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
              postParameters.add(new BasicNameValuePair("nama", nm.getText().toString()));
              postParameters.add(new BasicNameValuePair("nomor", nmr.getText().toString()));
              postParameters.add(new BasicNameValuePair("provider", type));
              postParameters.add(new BasicNameValuePair("nominal", type1));
              postParameters.add(new BasicNameValuePair("jenis_pembayaran", type2));
              String response = null;

              try {

                 response = CustomHttpClient.executeHttpPost("http://appmob.pusku.com/save.php", postParameters);

                 String res = response.toString();

                 res = res.trim();

                 res = res.replaceAll("\s+","");

                 error.setText(res);

                 if (res.equals("1")) error.setText("Data Tersimpan");

                 else error.setText("Data Tersimpan Ke server");

              }

              catch (Exception e) {

                 error.setText(e.toString());

              }

              String nama1 = nm.getText().toString();
              String nama2 = nmr.getText().toString();
              String nama3 = type;
              String nama4 = type1;
              String nama5 = type2;

             String sms = nama1+"#"+nama2+"#"+nama3+"#"+nama4+"#"+nama5;

                      Intent    i = new Intent (Intent.ACTION_SENDTO,Uri.parse("sms:5556"));
                          i.putExtra("sms_body", sms);
                          startActivity(i); 

                                  nm.setText(null);
                                  nmr.setText(null);  

           }




      });

      }   
}

in permission i active that <uses-permission android:name="android.permission.INTERNET"/> and still not working

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to run internet activities on a thread separate from main (UI) thread, you can do that by wrapping your code in the following block:

new Thread() {
    @Override
    public void run() {
       //your code here
    }
}.start();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...