2012-01-20 12 views
0

私はWebサービスを初めて利用しています。私はWebサービスに自分のデータを保存していますが、Webサービスからそのデータを取得する方法はわかりません。私のWebサービスは、そのWebサービスで.netを使用しています。だから私はどのように私はurl.iからデータを取得することができます私はGoogleで検索が私は解決策を見つけることができませんでした助けてください。どのように私はアンドロイドのドットネットWebサービスからデータを取得できますか?

私は

package com.soap; 
import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 
import android.app.*; 
import android.os.*; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MyworldActivity extends Activity { 

    EditText name,uname,pass,num,mail,img; 
    Button save,back; 




    private static final String SOAP_ACTION = "http://localhost/service1/InsertUsertRegistrationDetails"; 

    private static final String METHOD_NAME = "InsertUsertRegistrationDetails"; 

    private static final String NAMESPACE = "http://localhost/service1"; 
    private static final String URL = "http://113.193.181.53/MyWorldApp/Service1.asmx"; 

    TextView tv; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     tv=(TextView)findViewById(R.id.text1); 

     name = (EditText)findViewById(R.id.name); 
     uname = (EditText)findViewById(R.id.uname); 
     pass = (EditText)findViewById(R.id.pass); 
     num = (EditText)findViewById(R.id.num); 
     mail = (EditText)findViewById(R.id.mail); 
     img = (EditText)findViewById(R.id.img); 
     save = (Button)findViewById(R.id.save); 
     back = (Button)findViewById(R.id.back); 
     back.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       finish(); 

      } 
     }); 
     save.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       call(); 

      } 

     }); 


    } 

    public void call() 
    { 
     try { 

      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

      request.addProperty("Name", name.getText().toString()); 
      request.addProperty("UserName", uname.getText().toString()); 
      request.addProperty("Password", pass.getText().toString()); 
      request.addProperty("MobileNumber", num.getText().toString()); 
      request.addProperty("EmailID", mail.getText().toString()); 
      request.addProperty("image",img.getText().toString()); 
      Log.e("success","success"); 
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      envelope.dotNet=true; 
      envelope.setOutputSoapObject(request); 

      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
      androidHttpTransport.call(SOAP_ACTION, envelope); 

      Object result = (Object)envelope.getResponse(); 

      tv.setText(result.toString()); 
     } catch (Exception e) { 
      tv.setText(e.getMessage()); 
      } 
    } 
} 

は、誰もが私はDOTNETのWebサービスからデータを取得することができますどのように私に言うことができる.. Webサービスに自分のデータを保存するために、次のコードを使用しています。

ありがとうございます。

+2

善良な君。私はどこから始めるのですか?まず、あなたの(非常に)公のウェブサイトをダウンして、あなたが実際に何かを得るまでローカルで仕事をします。第2に、コードから、名前、携帯電話番号、パスワード、電子メールを暗号化されていないリンクを通して単純に渡しているように見えます。なぜあなたがこれをしてはならないのかわからない場合は、勉強してください(または別の日の仕事を見つける)。 – adelphus

+1

また、UIスレッドでネットワークを呼び出さないでください。 – bschultz

答えて

1

SOAPリクエストの代わりに、私はHTTPリクエスト/レスポンスを利用できます。

以下のサンプルコードを参照してください。

/** 
* Connects to server. Sends data to server. Receive data from server. 
* 
* @param request 
*   IN parameter. Request string. 
* @param response 
*   OUT parameter, to get data which receives from server. 
* @param error 
*   OUT parameter, to get error information. 
* @return - Function status, true or false. 
*/ 
private boolean requestData(URI uri, StringBuilder responseString, StringBuilder error) { 
    HttpResponse response = null; 
    boolean isSuccess = true; 
    try { 
     HttpParams httpParameters = new BasicHttpParams(); 
     HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT); 
     HttpConnectionParams.setSoTimeout(httpParameters, SOCKET_TIMEOUT); 
     HttpGet request = new HttpGet(uri); 
     DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); 
     response = httpClient.execute(request);   

    } catch (ClientProtocolException e) { 
     error.append(e.getMessage()); 
     Log.e(Constants.LOG_TAG, "httpClient.execute() ClientProtocolException: " + e.getMessage()); 
     e.printStackTrace(); 
     isSuccess = false; 
    } catch (IOException e) { 
     error.append(e.getMessage()); 
     Log.e(Constants.LOG_TAG, "httpClient.execute() IOException: " + e.getMessage()); 
     e.printStackTrace(); 
     // Server connection is not OK. 
     TrendDataMgr.getInstance().setConnectionStatus(false); 
     isSuccess = false; 
    } catch (Exception e) { 
     error.append(e.getMessage()); 
     Log.e(Constants.LOG_TAG, "httpClient.execute() Exception: " + e.getMessage()); 
     e.printStackTrace(); 
     isSuccess = false; 
    } 

    // If response not needed, Exit. 
    if (null == responseString) { 
     return isSuccess; 
    } 

    if (response == null) { 
     Log.e(Constants.LOG_TAG, "requestData().httpClient.execute() failed. Response = null"); 
     return false; 
    } 

    // Server connection is OK. 

    try { 
     HttpEntity responseEntity = response.getEntity(); 
     String str = EntityUtils.toString(responseEntity); 
     responseString.append(str); 
     //Log.d(Constants.LOG_TAG, "httpClient.execute()" + str); 

    }catch(Exception e) { 
     error.append(e.getMessage()); 
     Log.e(Constants.LOG_TAG, "EntityUtils.toString(), Exception: " + e.getMessage()); 
     e.printStackTrace(); 
     isSuccess = false; 
    } 
    return isSuccess; 

} 

Uは、URLを使ってこの関数を呼び出すことができます。

String request = URL + "/" + URLEncoder.encode(parameter, "UTF-8").replace("+", "%20"); 
boolean isSuccess = true; 
URI uri = new URI(request); 
isSuccess = requestData(uri, response, error); 

希望、これはuのに役立ちます。

関連する問題