Right now i am consuming two webservices by the method SOAP on android.The way of consuming the webservice is just getting the input from the user and showing it in textviews.Well its working fine.But i’m trying to show that answer in tabs of a tabhost which consists of two tabs.Im trying to show the answer from the first webservice is to be shown in first tab of a tabhost and the answer from the second webservice is to be show in second tab of the same tabhost…
But here the problem is, its showing the same answer on the two tabs,how to overcome this problem?
NOTE: Im using the same edittext and button for getting the input from the user for both webservices.
I have four activities namely,
- Demo_tabActivity.java [main activity]
- tabhost.java
The below two activities are tabs of the above
- Tab_1.java
- Tab_2.java
The first activity(Demo_tabActivity.java) contains an edittext & button.The second(Tabhost.java) activity contains a Tabhost widget.The third & fourth activities contains textviews respectively.
The first activity is going to consume a web service by getting the input from the user and returns some data on first tab(third activity) of a tabhost(second activity).
Like wise it will consume the another webservice.
Please find my code below
Demo_tabActivity.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();
}
}
});
}
}
Tabhost.java
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);
}
}
Tab_1.java
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);
}
}
Tab_2.java
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);
}
}
Thanks for your help!..
There are few silly mistakes in your code. Please do the corrections:
Change the particular lines of code:
with
with
You will get the result.