4

Androidから.netWebサービスに入力データを渡す方法とAndroidアプリケーションに結果を取得する方法についての効果的な例/チュートリアルが必要です。

私のニーズに関連すると思われるガイド、例、チュートリアルへのリンクを共有してください。

4

2 に答える 2

1

Rest Webservice を作成できます。ここにチュートリアルがあります。URIを使用して入力データを渡すことができます。たとえば、顧客名を投稿するには、URI をどのようにするかを計画する必要があります。

        [OperationContract]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/post-customer/{name}")]
        void postCustomer(string name);

顧客 ID を使用して顧客データを取得するには:

        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/get-customer/{id}")]
        Customer getCustomer(string id);

次に、Web サービスをホストした後、たとえば HTTP クライアントを使用して Android アプリからアクセスする必要があります。

String uri = "uri to your service";
HttpClient httpclient = new DefaultHttpClient();  
HttpGet request = new HttpGet(uri);  
ResponseHandler<String> handler = new BasicResponseHandler();  
String result = null;
try {  
    result = httpclient.execute(request, handler);  
} catch (ClientProtocolException e) {  
    e.printStackTrace();  
} catch (IOException e) {  
    e.printStackTrace();  
}  
httpclient.getConnectionManager().shutdown();

その後、「result」に文字列が含まれている必要があり、この文字列は応答 (json または xml) を表します。

この情報がお役に立てば幸いです。

于 2012-05-06T07:35:11.687 に答える
0

ここで小さなサンプルを与えて、投稿用にこれを試してください

public void post(String appid,String custlogid,String cityareaid,String mosqname,String mosqid,String lat,String lon){

        HttpClient httpclient = new DefaultHttpClient();   
        HttpPost httppost = new HttpPost(globalconstant.URL_mosquepost); 
        UrlEncodedFormEntity form;
        // Add your data   
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);   
        nameValuePairs.add(new BasicNameValuePair("APPUDID", appid));
        nameValuePairs.add(new BasicNameValuePair("CUSTLOGID", custlogid));
        nameValuePairs.add(new BasicNameValuePair("CITYAREAID", cityareaid));
        nameValuePairs.add(new BasicNameValuePair("CITYMOSQUENAME", mosqname));
        nameValuePairs.add(new BasicNameValuePair("CITYMOSQUEID", "0"));
        nameValuePairs.add(new BasicNameValuePair("LATITUDE", lat));
        nameValuePairs.add(new BasicNameValuePair("longitude", lon));
        try {
            form = new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
             httppost.setEntity(form);
            Log.d("myapp", "works till here. 2");
            try {
                HttpResponse response = httpclient.execute(httppost);
                Log.d("myapp", "response " + response.getStatusLine().getStatusCode()+"\n"+EntityUtils.toString(response.getEntity()));

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
于 2012-06-03T14:42:22.117 に答える