I want to retrieve data from php file to my new android app. I tried to do it as background process there for I want to use it without UI thread. I couldn't remove UI thread from this code any one hep me to remove UI thread in this code do it?
I try it but there is lot of red underlines. Actually I try to get data from host using a AlarmManager in every 5 seconds
my AlarmDemo class
public class AlarmDemo extends Activity {
Toast mToast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_demo);
// repeated alerm
Intent intent = new Intent(AlarmDemo.this, RepeatingAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmDemo.this, 0,
intent, 0);
// We want the alarm to go off 5 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += 5 * 1000;
// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
5 * 1000, sender);
// Tell the user about what we did.
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(AlarmDemo.this, "Rescheduled",
Toast.LENGTH_LONG);
mToast.show();
// end repeatdalarm
}
}
my RepeatingAlarm class
public class RepeatingAlarm extends BroadcastReceiver {
Context context;
Button b;
EditText et,pass;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
private AutoCompleteTextView autoComplete;
private MultiAutoCompleteTextView multiAutoComplete;
private ArrayAdapter<String> adapter;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "OK", Toast.LENGTH_SHORT).show();
dialog = ProgressDialog.show(RepeatingAlarm.this, "",
"Validating user...", true);
new Thread(new Runnable() {
public void run() {
login();
}
}).start();
}
void login(){
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://androidexample.com/media/webservice/getPage.php");
//add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
String text = response;
tv.setText("Response from PHP : " + text);
dialog.dismiss();
}
});
}catch(Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
}
please help me I'm new in android