Showing posts with label Android HttpPost. Show all posts
Showing posts with label Android HttpPost. Show all posts

Thursday, December 26, 2013

Send request on server using http post in Android

 class AnyntaskToSendData extends AsyncTask<String, Void, Integer> {
ProgressDialog dialog1;

@Override
protected void onPreExecute() {
dialog1 = ProgressDialog.show(TestActivity.this, "",
"Loading...");
super.onPreExecute();
}

@Override
protected void onPostExecute(Integer result) {
dialog1.dismiss();
Intent in = new Intent(TestActivity.this, DestActivity.class);
startActivity(in);
super.onPostExecute(result);
}

@Override
protected Integer doInBackground(String... params) {
//Getting email and mobile from phone
try {
AccountManager am = AccountManager.get(TestActivity.this);
Account[] accounts = am.getAccounts();

for (Account ac : accounts) {
String acname = ac.name;
actype = ac.type;
boolean isEmail = checkEmail(acname);
if (isEmail) {
emailId = acname;
}
if(emailId == null){
emailId = "faiz@gmail.com";
}
// Take your time to look at all available accounts
if (acname.startsWith("91")) {
number = acname;
}
if(number == null){
number = "7411415755";
}

}

} catch (Exception e) {
e.printStackTrace();
}
try{

String storeDataUrl = "http://oxyandroid.blogspot.com/postRequest.php?request_parameter=";
String url1="%7B"+"%22EmailId%22%3A"+"%22"+emailId.trim()+"%22"+"%2C"+"%22MobileNo%22%3A"+"%22"+number.trim()+"%22%7D";
String con_url=convertURL(url1);
System.out.println("con_url====="+con_url);
String freshurl=storeDataUrl.trim()+con_url.trim();
System.out.println("freshurl====="+freshurl);
JSONParser jParser = new JSONParser();

JSONObject json = jParser.getJSONFromUrl(freshurl);
JSONObject webobj = json.getJSONObject("webservice");
boolean status=webobj.getBoolean("system_status");

} catch (JSONException e) {
e.printStackTrace();
}
return null;
}

}
//creating valid url for post
 public static String convertURL(String str) {

   String url = null;
   try{
   url = new String(str.trim().replace(" ", "%20").replace("&", "%26")
           .replace(",", "%2c").replace("(", "%28").replace(")", "%29")
           .replace("!", "%21").replace("=", "%3D").replace("<", "%3C")
           .replace(">", "%3E").replace("#", "%23").replace("$", "%24")
           .replace("'", "%27").replace("*", "%2A").replace("-", "%2D")
           .replace(".", "%2E").replace("/", "%2F").replace(":", "%3A")
           .replace(";", "%3B").replace("?", "%3F").replace("@", "%40")
           .replace("[", "%5B").replace("\\", "%5C").replace("]", "%5D")
           .replace("_", "%5F").replace("`", "%60").replace("{", "%7B")
           .replace("|", "%7C").replace("}", "%7D"));
   }catch(Exception e){
       e.printStackTrace();
   }
   return url;
}
//checking valid email using pattern

public final Pattern EMAIL_ADDRESS_PATTERN = Pattern
.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + "\\@"
+ "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + "(" + "\\."
+ "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + ")+");

private boolean checkEmail(String email) {
return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
}

////////////////////////////////////JSON Parsor Class////////////////////////////////////////////////////////////////////////////////////////////

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url) {

// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String
return jObj;

}
}