How i upload my battery level data to my web server with service?I try this 
code but no work...any help?What is wrong?

     

public class BatteryLevel extends Activity {

private TextView contentTxt;

String strUrlup ="http:/..../bat_android.php";
String textbat;

  private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(final Context context, Intent intent) {
      // TODO Auto-generated method stub
      int level = intent.getIntExtra("level", 0);
      contentTxt.setText(String.valueOf(level) + "%");

       textbat = String.valueOf(level) + "%";

                Toast.makeText(context, textbat, Toast.LENGTH_SHORT).show();

                Intent service = new Intent(context, UploadService.class);
                service.putExtra("url", strUrlup);
                    service.putExtra("batlevel",textbat); 
                context.startService(service);
            };


  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    contentTxt = (TextView) this.findViewById(R.id.txt);
    this.registerReceiver(this.mBatInfoReceiver, 
    new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
  }
 }

public class UploadService extends IntentService {

          private int result = Activity.RESULT_CANCELED;

          public UploadService() {
            super("UploadService");
          }

          // Will be called asynchronously be Android
          @Override
          protected void onHandleIntent(Intent intent) {
            
            String url = intent.getStringExtra("url");
            
            String bat=intent.getStringExtra("batlevel");
        
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(url);
            try {
              List<NameValuePair> nameValuePairs = new 
ArrayList<NameValuePair>(1);
              nameValuePairs.add(new BasicNameValuePair("bat_and_lev",bat));
              post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
         
              HttpResponse response = client.execute(post);
              BufferedReader rd = new BufferedReader(new 
InputStreamReader(response.getEntity().getContent()));
              String line = "";
              while ((line = rd.readLine()) != null) {
                System.out.println(line);
              }

            } catch (IOException e) {
              e.printStackTrace();
            }
            //return null;
            
            
              }
              // Sucessful finished
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to