从android中的WCF webservices获取响应 [英] get Response from WCF webservices in android

查看:52
本文介绍了从android中的WCF webservices获取响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i有一个带有xml响应的网络服务如何获得响应并存储这些值。

如果有人提供简单的例子对我很有帮助



提前谢谢

Hi,

i have a web services with xml response how to get response and store that values.
if anybody provie simple example great help for me

THanks in advance

推荐答案

我有多少次回答你? !好的,看到这个:

在这里我收到来自WCF的回答评论。

Android:

How many times I have to answer you ?!! Okay, see this :
Here I'm getting comments for answer from WCF.
Android :
package com.example.qnaforum;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;



public class CommentHelper extends AsyncTask<String, String, ArrayList<HashMap<String, String>>>
{
	Reader reader;
	JSONArray jar;
	JSONObject obj;
	HttpEntity entity;
	HttpResponse response;
	
	ArrayList<HashMap<String, String>> CommentsList;
	
	Context ctx;
	Server server;
	
	int ansId;
	
	public CommentHelper(Context ctx)
	{
		this.ctx = ctx;
		server = new Server(ctx);
		CommentsList = new ArrayList<HashMap<String,String>>();
	}
	
	public CommentHelper(Context ctx, int ansId)
	{
		this.ctx = ctx;
		this.ansId = ansId;
		server = new Server(ctx);
		CommentsList = new ArrayList<HashMap<String,String>>();
	}
	
	@Override
    protected void onPreExecute()
	{
        super.onPreExecute();
       
        try
        {
        	//CommentsList.removeAll(get());
        	CommentsList.clear();
        }
        catch(Exception ex)
        {
        	Log.e("Clear comment list : ", ex.toString());
        }
    }
	
	@Override
	protected ArrayList<HashMap<String, String>> doInBackground(String... params) 
	{
		try
		{	
			DefaultHttpClient client = new DefaultHttpClient();
	    	HttpGet request = new HttpGet(server.GetServerIP() + "GetComments?aId=" + ansId);
	    	  	
	    	request.setHeader("Accept", "application/json");
	    	request.setHeader("Content-type", "application/json");
	       
	    	response = client.execute(request); 
	        entity = response.getEntity();   
	        
	        if(entity.getContentLength() != 0) 
	        {
	        	reader = new InputStreamReader(entity.getContent(),"UTF-8");
	        	char[] buffer = new char[(int) entity.getContentLength()];
	        	reader.read(buffer);
	        	reader.close();
	        	
	        	jar = new JSONArray(new String(buffer));
	        	for(int i=0; i<jar.length(); i++)
	        	{
	        		HashMap<String, String> item = new HashMap<String, String>();
		    	   	obj = jar.getJSONObject(i);
		    	   	
		    	   	item.put("COMMENTID", obj.getString("COMMENTID"));
		    	   	item.put("COMMENT", obj.getString("COMMENT"));
		    	   	item.put("ADDEDDATE", obj.getString("ADDEDDATE"));
		    	   	item.put("ANSWER", obj.getString("ANSWER"));
		    	   	item.put("USERNAME", obj.getString("USERNAME"));
		    	   	item.put("USERCLASS", obj.getString("USERCLASS"));
		    	   	
			    	CommentsList.add(item);
	        	} 	
	        }
	        else
	        {
	        	// no record found...
	        }
	      
		}
		catch(Exception ex)
		{
			Log.i("COMMENT ERROR : ", ex.toString());
		}
		return CommentsList;
	}
}



WCF接口:


WCF Interface:

/////      COMMENTS     //////
[OperationContract]
[WebGet(UriTemplate = "GetComments?aId={ansId}",
    BodyStyle = WebMessageBodyStyle.WrappedRequest,
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json)]
Comment[] GetComments(int ansId);

// implementation of this method is not shown here... 





最重要的部分是归因于方法。

我希望你现在有了这个想法:): )

此代码完美无缺。



-KR



The most important part is that attributing the method.
I hope now you got the idea :) :)
This code works perfectly.

-KR


这篇关于从android中的WCF webservices获取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆