在主线程异常Android操作系统的网络 [英] android os network on main thread exception

查看:142
本文介绍了在主线程异常Android操作系统的网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个应用程序的工作这code,但如果与其他应用一起,显示此对话框Android操作系统的网络上的主线程异常


 包com.kelompok2.bissmilahpulsa;
    进口的java.util.ArrayList;
    进口org.apache.http.NameValuePair;
    进口org.apache.http.message.BasicNameValuePair;
    进口android.net.Uri;
    进口android.os.Bundle;
    进口android.app.Activity;
    进口android.content.Intent;
    进口android.view.Menu;
    进口android.view.View;
    进口android.widget.Button;
    进口android.widget.EditText;
    进口android.widget.RadioGroup;
    进口android.widget.TextView;    公共类科巴延伸活动{
      的EditText纳米,核磁共振;
      RadioGroup中省,NOM,PEM;
      TextView的错误;
      OK按钮;      @覆盖  保护无效的onCreate(捆绑savedInstanceState){
      super.onCreate(savedInstanceState);
      的setContentView(R.layout.mulaitransaksi);      纳米=(EditText上)findViewById(R.id.et_nama);
      核磁共振=(EditText上)findViewById(R.id.et_nomor);
      省=(RadioGroup中)findViewById(R.id.provider);
      NOM =(RadioGroup中)findViewById(R.id.nominal);
      误差=(的TextView)findViewById(R.id.error);
      PEM =(RadioGroup中)findViewById(R.id.pembayaran);
      OK =(按钮)findViewById(R.id.btn_ok);      ok.setOnClickListener(新View.OnClickListener(){          @覆盖
          公共无效的onClick(视图v){
              // TODO自动生成方法存根
              字符串类型= NULL;
               字符串类型1 = NULL;
               字符串类型2 = NULL;
               开关(prov.getCheckedRadioButtonId())
               {
               案例R.id.rb_as:
               键入=作为;
               打破;
               案例R.id.rb_axis:
               键入=轴;
               打破;
               案例R.id.rb_im3:
               类型=IM3;
               打破;
               案例R.id.rb_telkomsel:
               类型=Telokmsel;
               打破;
               案例R.id.rb_xl:
               类型=XL;
               打破;
               }
               开关(nom.getCheckedRadioButtonId())
               {
               案例R.id.rb_5:
               TYPE1 =5;
               打破;
               案例R.id.rb_10:
               类型1 =10;
               打破;
               案例R.id.rb_20:
               类型1 =20;
               打破;
               案例R.id.rb_50:
               TYPE1 =50;
               打破;
               }
               开关(pem.getCheckedRadioButtonId())
               {
               案例R.id.rb_cash:
               TYPE2 =现金;
               打破;
               案例R.id.rb_hutang:
               TYPE2 =湖塘;
               打破;
               }              ArrayList的<&的NameValuePair GT; postParameters =新的ArrayList<&的NameValuePair GT;();
              postParameters.add(新BasicNameValuePair(非农产品市场准入,nm.getText()的toString()));
              postParameters.add(新BasicNameValuePair(nomor,nmr.getText()的toString())。);
              postParameters.add(新BasicNameValuePair(提供者,类型));
              postParameters.add(新BasicNameValuePair(标称,TYPE1));
              postParameters.add(新BasicNameValuePair(jenis_pembayaran,2型));
              串响应=无效;              尝试{                 响应= CustomHttpClient.executeHttpPost(http://appmob.pusku.com/save.php,postParameters);                 字符串资源= response.toString();                 RES = res.trim();                 RES = res.replaceAll(\\\\ S +,);                 error.setText(RES);                 如果(res.equals(1))error.setText(数据Tersimpan);                 其他error.setText(数据Tersimpan柯服务器);              }              赶上(例外五){                 error.setText(e.toString());              }              。字符串nama1 = nm.getText()的toString();
              。字符串nama2 = nmr.getText()的toString();
              字符串nama3 =类型;
              字符串nama4 = TYPE1;
              字符串nama5 = 2型;             串短信= nama1 +#+ nama2 +#+ nama3 +#+ nama4 +#+ nama5;                      意图I =新意图(Intent.ACTION_SENDTO,Uri.parse(短信:5556));
                          i.putExtra(SMS_BODY,短信);
                          startActivity(ⅰ);                                  nm.setText(NULL);
                                  nmr.setText(NULL);           }
      });      }
}


在我的权限是活跃<使用许可权的android:NAME =android.permission.INTERNET对/>
和仍然没有工作。


解决方案

您需要在一个线程上运行的互联网活动,从主(UI)线程中分离出来,你可以做到这一点通过以下块包裹你的code:

 新的Thread(){
    @覆盖
    公共无效的run(){
       //你的code在这里
    }
}。开始();

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

解决方案

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

这篇关于在主线程异常Android操作系统的网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆