February 2nd, 2012, 01:17 AM
|
#1 (permalink)
|
|
New Member
Join Date: Feb 2012
Location: Indore
Posts: 7
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
|
call web service and get response in JSON
public class Caller {
String response1 = null;
public void login()
{
try{
HttpGet httpget = new HttpGet("www.google.com");
HttpParams httpParams = new BasicHttpParams();
DefaultHttpClient HttpClient = new DefaultHttpClient(httpParams);
HttpResponse response = HttpClient.execute(httpget);
response1 = InputStreamtoString(response.getEntity().getConten t());
try {
JSONObject rootobj = new JSONObject(response1);
String IsLogin = rootobj.get("IsLogin").toString();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (ClientProtocolException e) {
// TODO: handle exception
e.printStackTrace();
}
catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
public String InputStreamtoString(InputStream is)
{
String resp = null;
String s = null,line=null;
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try{
for(; ; rd.readLine())
{
if((line = rd.readLine())!=null)
{
s +=line;
}else
{
break;
}
}
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
return resp;
}
//------------------------------------------------------
// for adding cookies in request we can use in this way
//------------------------------------------------------
public ArrayList<ArrayList<String>> getfriendcomment()
{
ArrayList<ArrayList<String>> commentlist = new ArrayList<ArrayList<String>>();
try {
HttpGet httpGet = new HttpGet(path+"search/GetFriendsCommentsForACompany?idCompany="+All_data .Comp_id/*+"&uid=1&logoutfirst=1"*/);
HttpClient httpclient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookie(All_data.cookies.get(0));
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STO RE, cookieStore);
HttpResponse response = httpclient.execute(httpGet, localContext);
InputStream resdata = response.getEntity().getContent();
response1 =inputStreamToString(resdata);
JSONObject rootObj = new JSONObject(response1);
JSONArray result = (JSONArray) rootObj.get("result");
int count =result.length();
for(int j=0; j<count;j++)
{
ArrayList<String> comment_data = new ArrayList<String>();
JSONObject firstRoute = (JSONObject) result.getJSONObject(j);
comment_data.add(firstRoute.get("ID_COMMENT").toSt ring());//0
comment_data.add(firstRoute.get("USER_ID").toStrin g());//1
comment_data.add(firstRoute.get("COMPANY_ID").toSt ring());//2
comment_data.add(firstRoute.get("DATE_UPDATED").to String());//3
comment_data.add(firstRoute.get("COMMENT").toStrin g());//4
comment_data.add(firstRoute.get("PRIVATE").toStrin g());//5
comment_data.add(firstRoute.get("DATE_ADDED").toSt ring());//6
comment_data.add(firstRoute.get("SOURCE").toString ());//7
comment_data.add(firstRoute.get("ISLIKE").toString ());//8
JSONObject SecondRoute = (JSONObject) firstRoute.get("AuthorDetails");
if(SecondRoute.get("Name").toString().equals("null "))
{
comment_data.add("NULL");//9
}
else
{
comment_data.add(SecondRoute.get("Name").toString( ));//9
}
if(SecondRoute.get("FacebookUID").toString().equal s("null"))
{
comment_data.add("NULL");//10
}
else
{
comment_data.add(SecondRoute.get("FacebookUID").to String());//10
}
commentlist.add(comment_data);
//
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception er){
er.printStackTrace();
}
return commentlist;
}
//------------------------------------------------------
// for adding cookies in request we can use in this way
//------------------------------------------------------
}
|
|
|