【发布时间】:2025-12-20 20:05:16
【问题描述】:
我正在开发一个与 asp.net 网络服务连接的 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();
}
}
【问题讨论】:
标签: android web-services networkonmainthread