Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

1 solution

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 Interface:
C#
/////      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... 


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

-KR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900