我正在开发一个连接asp.net web服务的android应用程序..当我测试应用程序时显示响应
Android OS在网络主线程异常“。
我的代码
class GetDetails extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading the result... Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args)
{
try
{
runOnUiThread(new Runnable() {
@Override
public void run()
{
TextView webserviceResponse = (TextView) findViewById(R.id.textView1);
webserviceResponse.setText("Requesting to server .....");
//Create Webservice class object
WebserviceCall com = new WebserviceCall();
// Initialize variables
String weight = "18000";
String fromUnit = "Grams";
String toUnit = "Kilograms";
//Call Webservice class method and pass values and get response
String aResponse = com.getConvertedWeight("ConvertWeight", weight, fromUnit, toUnit);
//Alert message to show webservice response
Toast.makeText(getApplicationContext(), weight+" Gram= "+aResponse+" Kilograms",
Toast.LENGTH_LONG).show();
Log.i("AndroidExampleOutput", "----"+aResponse);
webserviceResponse.setText("Response : "+aResponse);
}
}
);
}
finally {
}
return null;
}
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
答案 0 :(得分:1)
将您的所有代码从runOnUiThread(new Runnable() {...}
移至doInBackground(...)
由于runOnUiThread(..)
代码在主线程中执行
还initialized
Views
Activity onCreate(..)
中的class GetDetails extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading the result... Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args)
{
try
{
webserviceResponse.setText("Requesting to server .....");
//Create Webservice class object
WebserviceCall com = new WebserviceCall();
// Initialize variables
String weight = "18000";
String fromUnit = "Grams";
String toUnit = "Kilograms";
//Call Webservice class method and pass values and get response
String aResponse = com.getConvertedWeight("ConvertWeight", weight, fromUnit, toUnit);
Log.i("AndroidExampleOutput", "----"+aResponse);
return aResponse;
}
}
return null;
}
}
protected void onPostExecute(String aResponse) {
// dismiss the dialog once got all details
pDialog.dismiss();
//Alert message to show webservice response
Toast.makeText(getApplicationContext(), weight+" Gram= "+aResponse+" Kilograms",
Toast.LENGTH_LONG).show();
webserviceResponse.setText("Response : "+aResponse);
}
}
var phone2 = ^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$;
答案 1 :(得分:0)
在 doInBackground()中,您编写了 runonUiThread()方法。
在 runOnUIThread()内部,您正在尝试进行网络通话。这就是为什么它提供 NetworkOnMainThreadException 。
将该网络电话放在 runOnUiThread() 字符串aResponse = com.getConvertedWeight(&#34; ConvertWeight&#34;,weight,fromUnit,toUnit); 但在里面 doInBackground()我希望它能运作。
答案 2 :(得分:0)
嗨使用处理程序更新UI。 处理程序示例
private Handler handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) {
switch( msg.what ){
case MSG:
progressDialog.show();
break;
case DETACH:
progressDialog.dismiss();
break;
}
return false; } });
在后台调用
Message m=Message.obtain();
prepareMessage(m);
handler.sendMessage(m);
public void prepareMessage(Message m)
{
Bundle b = new Bundle();
b.putString("message", "Activity Done in background!!!!!!!");
m.setData(b);
}