gpt4 book ai didi

主线程异常上的android os网络

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:05 24 4
gpt4 key购买 nike

我让这段代码在单个应用程序中工作,但如果加入其他应用程序,则显示此对话框“主线程异常上的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com