2012-05-30 32 views
-1

任何人都可以告訴我如何從android eclipse上的web服務中檢索特定數據。 我已經完成了這些事情,請告訴我如何修正這個錯誤。在android eclipse上從webservice檢索特定數據

請在下面找到我的來源。

[Web方法]

package com.android.backend; 

public class FahrenheitToCelsius { 

    public double FahrenheitToCelsius(double str){ 
     return ((str-32)*5)/9; 
    } 
} 

[SCREENACTIVITY.JAVA]

package com.android.button.web; 

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class Tesing_webserviceActivity extends Activity 
{ 
    /** Called when the activity is first created. */ 
    private static String NAMESPACE = "http://tempuri.org/"; 
    private static String METHOD_NAME = "FahrenheitToCelsius"; 
    private static String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius"; 
    private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL"; 

     Button btnFar,btnClear; 
     EditText txtFar,txtres; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     btnFar = (Button)findViewById(R.id.btn_getvalues); 

     btnClear = (Button)findViewById(R.id.btnClear); 
     txtFar = (EditText)findViewById(R.id.txtFar); 
     txtres = (EditText)findViewById(R.id.txtresult); 

     btnFar.setOnClickListener(new View.OnClickListener() 
     { 

      public void onClick(View v) 
      { 
      //Initialize soap request + add parameters 
      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);  

      //Use this to add parameters 
      request.addProperty("str",txtFar.getText().toString()); 

      //Declare the version of the SOAP request 
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

      envelope.setOutputSoapObject(request); 
      envelope.dotNet = true; 

      try { 
       HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

       //this is the actual part that will call the webservice 
       androidHttpTransport.call(SOAP_ACTION, envelope); 

       // Get the SoapResult from the envelope body. 
       SoapObject result = (SoapObject)envelope.bodyIn; 

       if(result != null) 
       { 
       //Get the first property and change the label text 
        txtres.setText(result.getProperty(0).toString()); 
        } 
        else 
        { 
         Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show(); 
        } 
        } catch (Exception e) { 
         e.printStackTrace(); 
       } 
       } 
      }); 

     btnClear.setOnClickListener(new View.OnClickListener() 
     { 

        public void onClick(View v) 
        { 
         txtres.setText(""); 
         txtFar.setText(""); 
        } 
      }); 
    } 
} 

非常感謝!! .....

+0

,什麼是確切的問題?你會得到什麼,你期望得到什麼? – mihail

+1

我有一個web服務,其中包含一些data.ok.Here我的需要是從android/eclipse使用web服務,所以我想通過edittextbox從用戶獲得輸入,並根據用戶輸入顯示適當的值android eclipse。 –

回答

1

如果你簡單檢索從您的web服務信息,我會建議使用返回JSON對象的REST服務。 在Android中,您可以使用GSON庫輕鬆解析這個JSON對象。 一種解析JSON到Java對象的實例:

objectType = new TypeToken<YourClass>(){}.getType(); 
//Parse the respons with GSON 
Gson gson = new Gson(); 
return gson.fromJson(webserviceResponse, objectType); 

可以通過使用一個簡單的HTTP GET請求容易地訪問該web服務。

  HttpClient client = new DefaultHttpClient(); 
     client.getParams().setParameter(CoreProtocolPNames.USER_AGENT,"android"); 
     HttpGet request = new HttpGet(); 
     request.setHeader("Content-Type", "text/plain; charset=utf-8"); 
     request.setHeader("Cache-Control", "no-cache"); 

     request.setURI(new URI(URL)); 
     HttpResponse response = client.execute(request); 
     in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 

     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 

     String NL = System.getProperty("line.separator"); 
     while ((line = in.readLine()) != null) 
     { 
      sb.append(line + NL); 
     } 
     in.close(); 
     String webserviceResponse = sb.toString(); 
0

我用我的項目之一,此功能(你可能不需要授權頭):

private static InputStream sendRequest(String requestContent, String serviceUrl, String SoapAction) throws Throwable { 

    // initialize HTTP post 
    HttpPost httpPost = null; 
    try { 
    httpPost = new HttpPost(serviceUrl); 

    httpPost.addHeader("Authorization", getB64Auth()); 
    httpPost.addHeader("Content-Type", "text/xml; charset=ISO-8859-1");//ISO-8859-1 ; UTF-8  
    httpPost.addHeader("SOAPAction", SoapAction); 
    } catch (Throwable e) { 
     Log.e(LOG_TAG, "Error initializing HTTP post for SOAP request", e); 
     throw e; 
    } 

    // load content to be sent 
    try { 
     HttpEntity postEntity = new StringEntity(requestContent); 
     httpPost.setEntity(postEntity);   
    } 
    catch (UnsupportedEncodingException e) { 
      Log.e(LOG_TAG, "Unsupported encoding of content for SOAP request", e); 
      throw e; 
     } 

    // send request 
    HttpResponse httpResponse = null; 
    HttpClient httpClient = new DefaultHttpClient(); 
    try { 
     httpResponse = httpClient.execute(httpPost); 
    } catch (Throwable e) { 
     Log.e(LOG_TAG, "Error sending SOAP request", e); 
     throw e;   
    } 

    // get SOAP response 
    try { 
     // get response code 
     int responseStatusCode = httpResponse.getStatusLine().getStatusCode(); 

     // if the response code is not 200 - OK, or 500 - Internal error, 
     // then communication error occurred 
     if (responseStatusCode != 200 && responseStatusCode != 500) { 
     Log.i(LOG_TAG, "Got SOAP response code " + responseStatusCode + " " 
      + httpResponse.getStatusLine().getReasonPhrase()); 
     } 

     // get the response content 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     InputStream is = httpEntity.getContent(); 
     return is; 
    } catch (Throwable e) { 
     Log.e(LOG_TAG, "Error getting SOAP response", e); 
     throw e; 
    } 
    }