Monday, 20 January 2014

PostParseGet FOR JSON

public class PostParseGet {

public Context mContext;
public boolean isNetError = false;
public boolean isOtherError = false;
public boolean isDeviceToken = false;
List<NameValuePair> nameValuePairs;
Object mFillObject;

ConnectivityManager mConnectivityManager;
NetworkInfo mNetworkInfo;

Gson mGson;

public PostParseGet(Context context) {
mContext = context;
mGson = new Gson();
}

public Object getmFillObject() {
return mFillObject;
}

public void setmFillObject(Object mFillObject) {
this.mFillObject = mFillObject;
}

public boolean isDeviceToken() {
return isDeviceToken;
}

public void setDeviceToken(boolean isDeviceToken) {
this.isDeviceToken = isDeviceToken;
}

/**
* Function checks internet if avail, Post data to particular Url and filled
* json object relating to the response.
*
* @param string
* @param nameValuePairs
* @param object
* @param context
* @return object
* @throws CustomException
* */
public Object postHttpURL(String url, List<NameValuePair> nameValuePairs, Object mObject)
{
HttpPost httppost;
HttpParams httpParameters;
int timeoutConnection = 20000;
HttpClient httpclient = null;
HttpResponse response = null;
String data = "";
isOtherError = false;


mFillObject= null;

if(check_Internet())
{
try {
mFillObject = mObject.getClass().newInstance();

httppost = new HttpPost(url);
httpParameters = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(httpParameters, timeoutConnection);
httpclient = new DefaultHttpClient(httpParameters);


MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

System.out.println("url...."+url);
if (nameValuePairs != null)
{
for(int index=0; index < nameValuePairs.size(); index++)
{
String paramName=nameValuePairs.get(index).getName();
String paramValue=nameValuePairs.get(index).getValue();

if(paramName.equalsIgnoreCase(Tags.NPV_Photo) || paramName.equalsIgnoreCase(Tags.NPV_Video) || paramName.equalsIgnoreCase(Tags.NPV_Audio))
{
if(paramValue.length()>0)
{
entity.addPart(paramName, new FileBody(new File (paramValue)));
}
}
else
{
entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));

}
}
httppost.setEntity(entity);
}

// Execute HTTP Post Request
response = httpclient.execute(httppost);
data = EntityUtils.toString(response.getEntity());


System.out.println("data...."+data);

if(data.equalsIgnoreCase("{\"is_device_deleted\":true}"))
setDeviceToken(true);

mFillObject = mGson.fromJson(data, mFillObject.getClass());
} catch (Exception e) {
isOtherError = true;
}
}
return mFillObject;
}




/**
* Function
* Takes and keeps a reference of the passed context in order to Check whether Internet is available or not.
* @param context
* @return The return will be true if the internet is available and false if not.
*/
public boolean check_Internet()
{
mConnectivityManager= (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();

if(mNetworkInfo != null && mNetworkInfo.isConnectedOrConnecting())
{
isNetError = false;
return true;
}
else
{
isNetError = true;
return false;
}
}


/**
 * Login.
 * @param sync_date
 * @param username
 * @param password
 * @param object
 * @return
 */
public Object login(String sync_date,String username,String password,Object object) {
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("sync_date", sync_date));
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));

return postHttpURL(<URL>,nameValuePairs, object);
}
}

No comments:

Post a Comment