我想檢索數據從PHP文件到我的新的Android應用程序。我試圖做它作爲後臺進程,因爲我想在沒有UI線程的情況下使用它。我無法從此代碼中刪除UI線程任何人都可以在此代碼中刪除UI線程嗎?檢索數據沒有UI線程
我試了一下,但有很多紅色的下劃線。 其實我嘗試在每5秒
我AlarmDemo類來獲得從使用AlarmManager主機數據
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
}
}
我RepeatingAlarm類
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());
}
}
}
請幫助我,我在安卓
新