0

现在我正在通过android上的SOAP方法使用两个Web服务。使用Web服务的方式只是从用户那里获取输入并在textviews中显示它。它工作正常。但我试图在标签中显示答案由两个选项卡组成的选项卡主机。我试图显示来自第一个 Web 服务的答案将显示在选项卡主机的第一个选项卡中,而来自第二个 Web 服务的答案将显示在同一选项卡主机的第二个选项卡中...

但这里的问题是,它在两个选项卡上显示相同的答案,如何克服这个问题?

注意: 我使用相同的编辑文本和按钮从用户那里获取两个 Web 服务的输入。

我有四项活动,即

  • Demo_tabActivity.java [主要活动]
  • tabhost.java

以下两个活动是上面的选项卡

  • Tab_1.java
  • Tab_2.java

第一个活动(Demo_tabActivity.java)包含一个编辑文本和按钮。第二个(Tabhost.java)活动包含一个 Tabhost 小部件。第三个和第四个活动分别包含文本视图。

第一个活动将通过获取用户的输入来使用 Web 服务,并在选项卡主机(第二个活动)的第一个选项卡(第三个活动)上返回一些数据。同样明智的是,它将消耗另一个 Web 服务。

请在下面找到我的代码

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);

    }
 }

谢谢你的帮助!..

4

1 回答 1

1

您的代码中几乎没有愚蠢的错误。请进行更正:

更改特定的代码行:

  1) SoapPrimitive res = (SoapPrimitive)envelope.getResponse();

   SoapPrimitive res = (SoapPrimitive)env.getResponse();

  2)request.addProperty("Celsius",txtFar.getText().toString());

    req.addProperty("Celsius",txtFar.getText().toString());

你会得到结果。

于 2012-06-26T10:10:58.827 回答