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.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
{
}
}
catch(Exception ex)
{
Log.i("COMMENT ERROR : ", ex.toString());
}
return CommentsList;
}
}
WCF Interface:
[OperationContract]
[WebGet(UriTemplate = "GetComments?aId={ansId}",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
Comment[] GetComments(int ansId);
The most important part is that attributing the method.
I hope now you got the idea :) :)
This code works perfectly.
-KR