Saturday, August 18, 2012

Parser


package androidexperts.app.realestate;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.graphics.BitmapFactory;

import com.google.gson.Gson;

public class PropertyPostParse {

public Context mContext;
public boolean isNetError = false;

public boolean isNetError() {
return isNetError;
}

public void setNetError(boolean isNetError) {
this.isNetError = isNetError;
}

public PropertyPostParse() {

}

public PropertyPostParse(Context context) {
mContext = context;
}

/**
* 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 = 30000;
HttpClient httpclient = null;
HttpResponse response = null;
String data = "";
Gson mGson = new Gson();

Object mFillObject = null;

try {
mFillObject = mObject.getClass().newInstance();
isNetError = false;
httppost = new HttpPost(Url);
httpParameters = new BasicHttpParams();

HttpConnectionParams
.setSoTimeout(httpParameters, timeoutConnection);
httpclient = new DefaultHttpClient(httpParameters);

if (nameValuePairs != null) {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
// Execute HTTP Post Request
response = httpclient.execute(httppost);
data = EntityUtils.toString(response.getEntity());
System.out.println("Final Data :" + data);
mFillObject = mGson.fromJson(data, mFillObject.getClass());

} catch (IllegalAccessException e) {
isNetError = true;
System.out.println("IllegalAccessException : " + e.toString());
e.printStackTrace();
} catch (InstantiationException e) {
isNetError = true;
System.out.println("InstantiationException : " + e.toString());
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
isNetError = true;
System.out.println("Unsupported EncodingException : "
+ e.toString());
e.printStackTrace();
} catch (ClientProtocolException e) {
isNetError = true;
System.out.println("ClientProtocolException : " + e.toString());
e.printStackTrace();
} catch (IOException e) {
isNetError = true;
System.out.println("IOException : " + e.toString());
e.printStackTrace();
}

return mFillObject;
}

public String postHttpURL(String Url, List<NameValuePair> nameValuePairs) {

HttpPost httppost;
HttpParams httpParameters;
int timeoutConnection = 30000;
HttpClient httpclient = null;
HttpResponse response = null;
String data = "";
try {

httppost = new HttpPost(Url);
httpParameters = new BasicHttpParams();

HttpConnectionParams
.setSoTimeout(httpParameters, timeoutConnection);
httpclient = new DefaultHttpClient(httpParameters);

if (nameValuePairs != null) {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
// Execute HTTP Post Request
response = httpclient.execute(httppost);
data = EntityUtils.toString(response.getEntity());
System.out.println("Final Data :" + data);
} catch (Exception e) {
isNetError = true;
System.out.println("Exception : " + e.toString());
}

return data;
}

private String getResizedBitmap(String path, int REQUIRED_SIZE) {
try {
File f = new File(path);
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(new FileInputStream(f).getFD(),
null, o);
int scale = 1;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
if (width_tmp > REQUIRED_SIZE) {
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
} else
scale = 1;
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
BitmapFactory.decodeFileDescriptor(new FileInputStream(f).getFD(),
null, o2);
return path;
} catch (Exception e) {
isNetError = true;
}
return null;
}

/**
* method for send images to webservice
* @param nameValuePairs
* @param mObject
* @return
*/
public Object postHttpURL(List<NameValuePair> nameValuePairs, Object mObject) {
HttpPost httppost;
HttpParams httpParameters;
int timeoutConnection = 30000;
HttpClient httpclient = null;
HttpResponse response = null;
Object filledObject = null;
Gson gson;

try {
isNetError = false;

try {
filledObject = mObject.getClass().newInstance();
} catch (IllegalAccessException e) {
isNetError = true;
} catch (InstantiationException e) {
isNetError = true;
}
String Url = "http://192.168.0.225/real_estate/customerpropertys/add";

httppost = new HttpPost(Url);
httpParameters = new BasicHttpParams();

HttpConnectionParams
.setSoTimeout(httpParameters, timeoutConnection);
httpclient = new DefaultHttpClient(httpParameters);

MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);

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

String image = (String) paramName.subSequence(0,
paramName.length() - 1);

if (image.equalsIgnoreCase("image")) {
// If the key equals to "image", we use FileBody to
// transfer the data
if (paramValue.length() > 0) {
paramValue = getResizedBitmap(paramValue, 400);
entity.addPart(paramName, new FileBody(new File(
paramValue)));
}
} else {
// Normal string data
entity.addPart(nameValuePairs.get(index).getName(),
new StringBody(nameValuePairs.get(index)
.getValue()));
}
}
httppost.setEntity(entity);
}
// Execute HTTP Post Request
response = httpclient.execute(httppost);
String data = EntityUtils.toString(response.getEntity());

if (data.startsWith("[")) {
data = "{\"resources\": " + data + "}";
}
System.out.println("Final Data :" + data);
gson = new Gson();
filledObject = gson.fromJson(data, mObject.getClass());

} catch (Exception e) {
e.printStackTrace();
isNetError = true;
}

return filledObject;
}
}

No comments:

Post a Comment