Created
October 29, 2015 08:57
-
-
Save filipproch/71a28047a8007cab1c50 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void sendRequests() { | |
new DataSender().execute(); | |
} | |
public static class DataSender extends AsyncTask<Void, Void, Void> { | |
@Override | |
protected Void doInBackground(Void... params) { | |
db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null); | |
Cursor c=db.rawQuery("SELECT * FROM student", null); | |
if(c.getCount()==0){ | |
System.out.println("No SMS found"); | |
return; | |
} else if (c.moveToFirst()) { | |
for (int i = 0; i < c.getCount(); i++) { | |
Sender = c.getString(1); | |
TimeStamp = c.getString(2); | |
Mesg = c.getString(3); | |
readWebPage(c.getString(0)); //POST method | |
c.moveToNext(); | |
} | |
} | |
c.close(); | |
return null; | |
} | |
private boolean readWebPage(String id) { | |
// TODO Auto-generated method stub | |
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); | |
value = settings.getString("code", ""); | |
HttpClient httpClient = new DefaultHttpClient(); | |
HttpPost httpPost = new HttpPost("http://example.com/mail.asmx/send?"); | |
//Post Data | |
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(5); | |
nameValuePair.add(new BasicNameValuePair("id", value)); | |
nameValuePair.add(new BasicNameValuePair("type", "IN")); | |
nameValuePair.add(new BasicNameValuePair("source", Sender)); | |
nameValuePair.add(new BasicNameValuePair("time", TimeStamp)); | |
nameValuePair.add(new BasicNameValuePair("body", Mesg)); | |
//Encoding POST data | |
try{ | |
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair)); | |
}catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
System.out.println(e); | |
} | |
//making POST request. | |
try{ | |
HttpResponse response = httpClient.execute(httpPost); | |
String XmlString = EntityUtils.toString(response.getEntity()); | |
XmlString=XmlString.replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim(); | |
XmlString = XmlString.substring(XmlString.indexOf("[") + 1, XmlString.lastIndexOf("]")); | |
JSONObject jObj = new JSONObject(XmlString); | |
webResponse = jObj.getString("status"); | |
System.out.println("Web Response:" + webResponse); | |
if(webResponse.equals("ok")){ | |
if(id!=null){ | |
db.execSQL("DELETE FROM student WHERE id="+id); | |
} | |
else{ | |
System.out.println("No SMS found"); | |
} | |
return true; | |
} | |
return false; | |
}catch (Exception e) { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment