在android eclipse上显示tabhost上的某些数据时出现歧义
现在,我正在android上通过SOAP方法使用两个Web服务。使用Web服务的方法只是从用户那里获取输入并在文本视图中显示。它工作得很好。但我正在尝试在由两个选项卡组成的tabhost的选项卡中显示该答案。我试图显示来自第一个Web服务的答案显示在tabhost的第一个选项卡中,来自第二个Web服务的答案将显示在同一tabhost的第二个选项卡中 但问题是,它在两个标签上显示相同的答案,如何克服这个问题 注意:我使用相同的编辑文本和按钮来获取用户对两个Web服务的输入。 我有四个活动,在android eclipse上显示tabhost上的某些数据时出现歧义,android,web-services,android-tabhost,Android,Web Services,Android Tabhost,现在,我正在android上通过SOAP方法使用两个Web服务。使用Web服务的方法只是从用户那里获取输入并在文本视图中显示。它工作得很好。但我正在尝试在由两个选项卡组成的tabhost的选项卡中显示该答案。我试图显示来自第一个Web服务的答案显示在tabhost的第一个选项卡中,来自第二个Web服务的答案将显示在同一tabhost的第二个选项卡中 但问题是,它在两个标签上显示相同的答案,如何克服这个问题 注意:我使用相同的编辑文本和按钮来获取用户对两个Web服务的输入。 我有四个活动, De
- Demo_tabActivity.java[主活动]
- tabhost.java
- Tab_1.java
- Tab_2.java
public class Demo_tabActivity extends Activity
{
private static String NAMESPACE1 = "http://tempuri.org/";
private static String NAMESPACE2 = "http://tempuri.org/";
private static String METHOD_NAME1 = "FahrenheitToCelsius";
private static String METHOD_NAME2 = "CelsiusToFahrenheit";
private static String SOAP_ACTION1 = "http://tempuri.org/FahrenheitToCelsius";
private static String SOAP_ACTION2 = "http://tempuri.org/CelsiusToFahrenheit";
private static String URL1 = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
private static String URL2 = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
Button btnFar;
EditText txtFar;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFar = (Button)findViewById(R.id.button1);
txtFar = (EditText)findViewById(R.id.editText_in);
btnFar.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String b;
String b2;
//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE1, METHOD_NAME1);
SoapObject req = new SoapObject(NAMESPACE2, METHOD_NAME2);
//Use this to add parameters
request.addProperty("Fahrenheit",txtFar.getText().toString());
request.addProperty("Celsius",txtFar.getText().toString());
//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
SoapSerializationEnvelope env = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
env.setOutputSoapObject(req);
envelope.dotNet = true;
try
{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
HttpTransportSE androidHttpT = new HttpTransportSE(URL2);
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION1, envelope);
androidHttpT.call(SOAP_ACTION2, env);
// Get the SoapResult from the envelope body.
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
SoapPrimitive res = (SoapPrimitive)envelope.getResponse();
if (result != null && res != null)
{
//Get the first property and change the label text
b = result.toString();
b2 = res.toString();
Intent myIntent = new Intent(v.getContext(), Tabhost.class);
myIntent.putExtra("gotonextpage", b);
myIntent.putExtra("goto", b2);
startActivity(myIntent);
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
public class Tabhost extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
String result;
String result2;
Bundle extras = getIntent().getExtras();
Bundle extras2 = getIntent().getExtras();
if(extras!=null)
{
result = extras.getString("gotonextpage");
} else result = "Didnt work !" ;
if(extras2 !=null)
{
result2 = extras2.getString("goto");
} else result2 = "Didnt work !" ;
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Tab_1.class);
intent.putExtra("result", result);
spec = tabHost.newTabSpec("first").setIndicator("First").setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Tab_2.class);
intent.putExtra("result2", result2);
spec = tabHost.newTabSpec("second").setIndicator("Second").setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
public class Tab_1 extends Activity
{
TextView tv1;
String result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
result = extras.getString("result");
} else result = "didnt work";
tv1 = (TextView)findViewById(R.id.textView_main2);
tv1.setText(result);
}
}
public class Tab_2 extends Activity {
String result2;
TextView tv2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Bundle extras2 = getIntent().getExtras();
if(extras2 != null) {
result2 = extras2.getString("result2");
} else result2 = "didnt work";
tv2 = (TextView)findViewById(R.id.textView_main3);
tv2.setText(result2);
}
}
Tabhost.java
public class Demo_tabActivity extends Activity
{
private static String NAMESPACE1 = "http://tempuri.org/";
private static String NAMESPACE2 = "http://tempuri.org/";
private static String METHOD_NAME1 = "FahrenheitToCelsius";
private static String METHOD_NAME2 = "CelsiusToFahrenheit";
private static String SOAP_ACTION1 = "http://tempuri.org/FahrenheitToCelsius";
private static String SOAP_ACTION2 = "http://tempuri.org/CelsiusToFahrenheit";
private static String URL1 = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
private static String URL2 = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
Button btnFar;
EditText txtFar;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFar = (Button)findViewById(R.id.button1);
txtFar = (EditText)findViewById(R.id.editText_in);
btnFar.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String b;
String b2;
//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE1, METHOD_NAME1);
SoapObject req = new SoapObject(NAMESPACE2, METHOD_NAME2);
//Use this to add parameters
request.addProperty("Fahrenheit",txtFar.getText().toString());
request.addProperty("Celsius",txtFar.getText().toString());
//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
SoapSerializationEnvelope env = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
env.setOutputSoapObject(req);
envelope.dotNet = true;
try
{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
HttpTransportSE androidHttpT = new HttpTransportSE(URL2);
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION1, envelope);
androidHttpT.call(SOAP_ACTION2, env);
// Get the SoapResult from the envelope body.
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
SoapPrimitive res = (SoapPrimitive)envelope.getResponse();
if (result != null && res != null)
{
//Get the first property and change the label text
b = result.toString();
b2 = res.toString();
Intent myIntent = new Intent(v.getContext(), Tabhost.class);
myIntent.putExtra("gotonextpage", b);
myIntent.putExtra("goto", b2);
startActivity(myIntent);
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
public class Tabhost extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
String result;
String result2;
Bundle extras = getIntent().getExtras();
Bundle extras2 = getIntent().getExtras();
if(extras!=null)
{
result = extras.getString("gotonextpage");
} else result = "Didnt work !" ;
if(extras2 !=null)
{
result2 = extras2.getString("goto");
} else result2 = "Didnt work !" ;
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Tab_1.class);
intent.putExtra("result", result);
spec = tabHost.newTabSpec("first").setIndicator("First").setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Tab_2.class);
intent.putExtra("result2", result2);
spec = tabHost.newTabSpec("second").setIndicator("Second").setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
public class Tab_1 extends Activity
{
TextView tv1;
String result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
result = extras.getString("result");
} else result = "didnt work";
tv1 = (TextView)findViewById(R.id.textView_main2);
tv1.setText(result);
}
}
public class Tab_2 extends Activity {
String result2;
TextView tv2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Bundle extras2 = getIntent().getExtras();
if(extras2 != null) {
result2 = extras2.getString("result2");
} else result2 = "didnt work";
tv2 = (TextView)findViewById(R.id.textView_main3);
tv2.setText(result2);
}
}
选项卡1.java
public class Demo_tabActivity extends Activity
{
private static String NAMESPACE1 = "http://tempuri.org/";
private static String NAMESPACE2 = "http://tempuri.org/";
private static String METHOD_NAME1 = "FahrenheitToCelsius";
private static String METHOD_NAME2 = "CelsiusToFahrenheit";
private static String SOAP_ACTION1 = "http://tempuri.org/FahrenheitToCelsius";
private static String SOAP_ACTION2 = "http://tempuri.org/CelsiusToFahrenheit";
private static String URL1 = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
private static String URL2 = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
Button btnFar;
EditText txtFar;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFar = (Button)findViewById(R.id.button1);
txtFar = (EditText)findViewById(R.id.editText_in);
btnFar.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String b;
String b2;
//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE1, METHOD_NAME1);
SoapObject req = new SoapObject(NAMESPACE2, METHOD_NAME2);
//Use this to add parameters
request.addProperty("Fahrenheit",txtFar.getText().toString());
request.addProperty("Celsius",txtFar.getText().toString());
//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
SoapSerializationEnvelope env = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
env.setOutputSoapObject(req);
envelope.dotNet = true;
try
{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
HttpTransportSE androidHttpT = new HttpTransportSE(URL2);
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION1, envelope);
androidHttpT.call(SOAP_ACTION2, env);
// Get the SoapResult from the envelope body.
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
SoapPrimitive res = (SoapPrimitive)envelope.getResponse();
if (result != null && res != null)
{
//Get the first property and change the label text
b = result.toString();
b2 = res.toString();
Intent myIntent = new Intent(v.getContext(), Tabhost.class);
myIntent.putExtra("gotonextpage", b);
myIntent.putExtra("goto", b2);
startActivity(myIntent);
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
public class Tabhost extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
String result;
String result2;
Bundle extras = getIntent().getExtras();
Bundle extras2 = getIntent().getExtras();
if(extras!=null)
{
result = extras.getString("gotonextpage");
} else result = "Didnt work !" ;
if(extras2 !=null)
{
result2 = extras2.getString("goto");
} else result2 = "Didnt work !" ;
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Tab_1.class);
intent.putExtra("result", result);
spec = tabHost.newTabSpec("first").setIndicator("First").setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Tab_2.class);
intent.putExtra("result2", result2);
spec = tabHost.newTabSpec("second").setIndicator("Second").setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
public class Tab_1 extends Activity
{
TextView tv1;
String result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
result = extras.getString("result");
} else result = "didnt work";
tv1 = (TextView)findViewById(R.id.textView_main2);
tv1.setText(result);
}
}
public class Tab_2 extends Activity {
String result2;
TextView tv2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Bundle extras2 = getIntent().getExtras();
if(extras2 != null) {
result2 = extras2.getString("result2");
} else result2 = "didnt work";
tv2 = (TextView)findViewById(R.id.textView_main3);
tv2.setText(result2);
}
}
选项卡2.java
public class Demo_tabActivity extends Activity
{
private static String NAMESPACE1 = "http://tempuri.org/";
private static String NAMESPACE2 = "http://tempuri.org/";
private static String METHOD_NAME1 = "FahrenheitToCelsius";
private static String METHOD_NAME2 = "CelsiusToFahrenheit";
private static String SOAP_ACTION1 = "http://tempuri.org/FahrenheitToCelsius";
private static String SOAP_ACTION2 = "http://tempuri.org/CelsiusToFahrenheit";
private static String URL1 = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
private static String URL2 = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
Button btnFar;
EditText txtFar;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFar = (Button)findViewById(R.id.button1);
txtFar = (EditText)findViewById(R.id.editText_in);
btnFar.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String b;
String b2;
//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE1, METHOD_NAME1);
SoapObject req = new SoapObject(NAMESPACE2, METHOD_NAME2);
//Use this to add parameters
request.addProperty("Fahrenheit",txtFar.getText().toString());
request.addProperty("Celsius",txtFar.getText().toString());
//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
SoapSerializationEnvelope env = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
env.setOutputSoapObject(req);
envelope.dotNet = true;
try
{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
HttpTransportSE androidHttpT = new HttpTransportSE(URL2);
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION1, envelope);
androidHttpT.call(SOAP_ACTION2, env);
// Get the SoapResult from the envelope body.
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
SoapPrimitive res = (SoapPrimitive)envelope.getResponse();
if (result != null && res != null)
{
//Get the first property and change the label text
b = result.toString();
b2 = res.toString();
Intent myIntent = new Intent(v.getContext(), Tabhost.class);
myIntent.putExtra("gotonextpage", b);
myIntent.putExtra("goto", b2);
startActivity(myIntent);
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
public class Tabhost extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
String result;
String result2;
Bundle extras = getIntent().getExtras();
Bundle extras2 = getIntent().getExtras();
if(extras!=null)
{
result = extras.getString("gotonextpage");
} else result = "Didnt work !" ;
if(extras2 !=null)
{
result2 = extras2.getString("goto");
} else result2 = "Didnt work !" ;
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Tab_1.class);
intent.putExtra("result", result);
spec = tabHost.newTabSpec("first").setIndicator("First").setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Tab_2.class);
intent.putExtra("result2", result2);
spec = tabHost.newTabSpec("second").setIndicator("Second").setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
public class Tab_1 extends Activity
{
TextView tv1;
String result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
result = extras.getString("result");
} else result = "didnt work";
tv1 = (TextView)findViewById(R.id.textView_main2);
tv1.setText(result);
}
}
public class Tab_2 extends Activity {
String result2;
TextView tv2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Bundle extras2 = getIntent().getExtras();
if(extras2 != null) {
result2 = extras2.getString("result2");
} else result2 = "didnt work";
tv2 = (TextView)findViewById(R.id.textView_main3);
tv2.setText(result2);
}
}
谢谢你的帮助 您的代码中几乎没有愚蠢的错误。请做以下更正: 更改特定代码行:
1) SoapPrimitive res = (SoapPrimitive)envelope.getResponse();
与
与
您将得到结果。请发布应用程序的代码/布局xml以理解您的问题。@Sahana请再看一次@my question,我已经为您提供了所有需要。@Sahana您有什么想法吗!。。我能看到的是结果字符串b和b1都是一样的。。尝试打印字符串b和b1。