主线程异常上的android os网络

标签 android multithreading exception program-entry-point

我让这段代码在单个应用程序中工作,但如果加入其他应用程序,则显示此对话框“主线程异常上的 android 操作系统网络”

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

           }




      });

      }   
}

允许我激活 <uses-permission android:name="android.permission.INTERNET"/> 仍然无法正常工作

最佳答案

您需要在独立于主 (UI) 线程的线程上运行互联网 Activity ,您可以通过将代码包装在以下 block 中来实现:

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

关于主线程异常上的android os网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17365646/

相关文章:

android - Jenkins插件Google Play将Android APK上传到Google Play失败:nullpointerexception

java - 第一次在recyclerview上显示获取数据,但第二次打开recyclerview不显示数据

java.lang.RuntimeException : Unable to instantiate application, java.lang.ClassNotFoundException:

android - 如何在 Android 中获取默认的 CALENDAR_ID?

security - Facebook-C#-sdk:v5 安全异常

java - 是什么导致了 java.lang.ArrayIndexOutOfBoundsException 以及如何防止它?

Java - 使用经典for循环会出现并发修改异常吗?

c++ - STL 中不必要的锁定? (Visual C++ Express)

python - Pandas DataFrame 多线程没有性能提升

java - 黑莓 (Java) 中的线程间控制处理