HttpHelper is used like middle ware for getting response from server. here i am pasting one example how its communicate with HttpRequest class and how its call.
Helper Class for Attributes:
-->Now, See below code for how its call and used for getting data from HttpHelperClass.
here is the base class which extends in all activity for getting request response
}
import
java.io.StringWriter;
import
javax.xml.transform.Transformer;
import
javax.xml.transform.TransformerException;
import
javax.xml.transform.TransformerFactory;
import
javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import
org.w3c.dom.Document;
import org.w3c.dom.Element;
import
org.w3c.dom.NodeList;
import
android.app.ProgressDialog;
import
android.os.AsyncTask;
import android.util.Log;
import com.ipixcshare.Act_Main;
import com.ipixcshare.Act_SignUp;
import com.ipixcshare.utils.Constants;
import com.ipixcshare.utils.SignUp;
public class HttpHelper extends AsyncTask<String,
Integer, Long>
{
private ProgressDialog dialog;
private int requestNumber;
private String loadingMessage;
private Act_Main activity_signup;
private Document response_xml;
SignUp
signUp=new SignUp();
private WebAPIRequest webAPIRequest = new WebAPIRequest();
public HttpHelper(int request_num, Act_SignUp
activity, String msg)
{
activity_signup = activity;
requestNumber = request_num;
dialog = new ProgressDialog(activity_signup);
dialog.setCancelable(false);
loadingMessage = msg;
}
protected void onPreExecute()
{
if(dialog != null)
{
dialog.setMessage(loadingMessage);
dialog.show();
}
}
@Override
protected Long
doInBackground(String... value)
{
if (requestNumber == Constants.Registration_Code)
{
String
request = value[0];
response_xml = webAPIRequest.performPost(Constants.App_Url,
request);
if(response_xml != null)
{
Log.i("Registration
Respopnce:==",getStringFromDocument(response_xml));
NodeList
root = (NodeList) response_xml.getElementsByTagName("Registration");
Element
childnode_root = (Element) root.item(0);
signUp.success =
getValueFromNode(childnode_root, "success");
signUp.message =
getValueFromNode(childnode_root, "message");
}
}
else if (requestNumber == Constants.Favorites_Code)
{
String
request_fav = value[0];
response_xml = webAPIRequest.performPost(Constants.App_Url, request_fav);
if(response_xml != null)
{
//Log.i("Favorites
Respopnce:==",getStringFromDocument(response_xml));
NodeList
root = (NodeList) response_xml.getElementsByTagName("Favorites");
Element
childnode_root = (Element) root.item(0);
favorites.success =
getValueFromNode(childnode_root, "success");
if(favorites.success.equalsIgnoreCase("true"))
{
NodeList
nodelist_fav = (NodeList) childnode_root.getElementsByTagName("user");
for (int i = 0; i <
nodelist_fav.getLength(); i++)
{
Favorites
fav=new Favorites();
Element
ch_node1 = (Element) nodelist_fav.item(i);
try
{
fav.userid=getValueFromNode(ch_node1,"userid");
fav.username=getValueFromNode(ch_node1,"username");
fav.mobile=getValueFromNode(ch_node1,"mobile");
arr_fav.add(fav);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
}
return null;
}
protected void onPostExecute(Long
result)
{
if(dialog != null)
{
dialog.dismiss();
}
if(requestNumber == Constants.Registration_Code)
{
activity_signup.setBackApiResponse(Constants.Registration_Code,signUp);
}
else if (requestNumber == Constants.View_Request_Code)
{
activity_viewrequest.setBackApiResponse(Constants.View_Request_Code,arr_vr);
}
}
String
getValueFromNode(Element childNode, String tagName)
{
String
strValue = "";
try
{
Element
node = (Element)childNode.getElementsByTagName(tagName).item(0);
for(int
i=0;i<node.getChildNodes().getLength();i++)
{
strValue
= strValue.concat(node.getChildNodes().item(i).getNodeValue());
}
}
catch(Exception exp)
{
}
return strValue;
}
int parseIntValue(String
strValue)
{
int value=0;
if(strValue!=null &&
strValue.length()>0)
{
value
= Integer.parseInt(strValue);
}
return value;
}
float
parseFloatValue(String strValue)
{
float value=0.0f;
if(strValue!=null &&
strValue.length()>0)
{
value
= Float.parseFloat(strValue);
}
return value;
}
double
parseDoubleValue(String strValue)
{
double value=0.0f;
if(strValue!=null &&
strValue.length()>0)
{
value
= Double.parseDouble(strValue);
}
return value;
}
public String
getStringFromDocument(Document doc)
{
try
{
DOMSource
domSource = new DOMSource(doc);
StringWriter
writer = new StringWriter();
StreamResult
result = new StreamResult(writer);
TransformerFactory
tf = TransformerFactory.newInstance();
Transformer
transformer = tf.newTransformer();
transformer.transform(domSource,
result);
return writer.toString();
}
catch(TransformerException
ex)
{
ex.printStackTrace();
}
return null;
}
}
It May be case there some attribute present in Tag while your are parsing xml file so below is class for Parsing xml data with Attributes
Helper Class for Attributes:
public class HttpHelperTask
{
WebAPIRequest
Wapi=new WebAPIRequest();
Object
obj = null;
public Object
GetResponce(String Arg)
{
Document
response = Wapi.performPost(Constants.url,Arg);
ArrayList<VideosAttribute>
arr_videos = new ArrayList<VideosAttribute>();
ArrayList<VideosAttribute>
arr_Rootvideos = new ArrayList<VideosAttribute>();
try
{
Element
RootNode = (Element) response.getElementsByTagName("Videos").item(0);
arr_Rootvideos.add(setRootNodeToVideo(RootNode));
NodeList
NlistVideo = RootNode.getElementsByTagName("Video");
for (int j = 0; j <
NlistVideo.getLength(); j++)
{
VideosAttribute
VA = new VideosAttribute();
Element
childNode = (Element) NlistVideo.item(j);
VA
= setNodeToVideo(childNode);
arr_videos.add(VA);
}
arr_videos.trimToSize();
}
catch (Exception e)
{}
return obj = arr_videos;
}
//For Get Attribute
of XML element
public VideosAttribute
setNodeToVideo(Element childNode)
{
VideosAttribute
video_att = new VideosAttribute();
video_att.AssetID=childNode.getAttribute("AssetID");
video_att.AlbumID=childNode.getAttribute("AlbumID");
return video_att;
}
This
Method for GetValue:
public void GetGameResponce()
{
Element
nodevideo = (Element) response.getElementsByTagName("Games").item(0);
NodeList
nlistvideo = nodevideo.getElementsByTagName("Game");
for (int j = 0; j <
nlistvideo.getLength(); j++)
{
Game
game = new Game();
Element
childNode = (Element) nlistvideo.item(j);
game
= setNodeToquestion(childNode);
arr_game.add(game);
}
arr_game.trimToSize();
System.out.println("Arry
size"+
arr_game.size());
}
public Game
setNodeToquestion(Element childNode)
{
Game
game = new Game();
game.Name
= getValueFromNode(childNode, "Name") == null ? "": getValueFromNode(childNode, "Name");
game.ImageName
= getValueFromNode(childNode, "ImageName") == null ? "":
getValueFromNode(childNode, "ImageName");
game.shortDesc
= getValueFromNode(childNode,"shortDesc") == null ? "" :
getValueFromNode(childNode,"shortDesc");
game.Description
= getValueFromNode(childNode,"description") == null ? "" :
getValueFromNode(childNode,"description");
game.market=
getValueFromNode(childNode,"market") == null ? "" : getValueFromNode(childNode,"market");
game.website=
getValueFromNode(childNode,"website") == null ? "" : getValueFromNode(childNode,"website");
Constants.MKY_URL=game.market;
Constants.WEBSITE=game.website;
System.out.println("Name================="+game.Name);
System.out.println("ImageName============"+game.ImageName);
System.out.println("shortDesc============"+game.shortDesc);
System.out.println("description=========="+game.Description);
System.out.println("Mkt
Url=========="+game.market);
System.out.println("Website
Url=========="+game.website);
return game;
}
public class Act_Main extends Activity implements OnClickListener
{
public void setBackApiResponse(int requestNumber,Object
obj)
{
}
@Override
protected void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public class Act_SignUp extends Act_Main
{
public void setBackApiResponse(int requestNumber,Object
obj)
{
signUp=(SignUp) obj;
if(signUp.success.equalsIgnoreCase("True"))
{
Toast.makeText(Act_SignUp.this,signUp.message,Toast.LENGTH_LONG).show();
}
else
{
}
}
@Override
protected void onCreate(Bundle
savedInstanceState)
{
// TODO Auto-generated
method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HttpHelperTask httpHelper = new HttpHelperTask(100, TrainingNotes_Activity.this, "Loading...");
httpHelper.execute("XmlUrl");
}
}
I
will be happy if you will provide your feedback or follow this blog. Any suggestion
and help will be appreciated.
Thank
you :)
No comments:
Post a Comment