1

该项目在数据库中保存了一个数字(433),通过 servlet 访问该数字,然后应将其发送到 Android 应用程序。在我添加 AsyncTask 之前,应用程序有时会从 servlet 中检索数据(应该是 433),有时则不会。单击主页按钮后,应该会出现 mainActivity 屏幕并显示数据。我添加了 AsyncTask 希望它能够始终如一地工作,但现在我得到的只是文本“Light Data=”但没有数据(应该说“Light Data=433)。servlet 连接到数据库很好,所以我知道问题出在应用程序。我非常感谢与我的项目相关的代码,谢谢。

主页:

package com.example.clearlight;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class HomePage extends Activity {

    private Button ScheduleBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homepage);

         ScheduleBtn = (Button) findViewById(R.id.home_btn);

        ScheduleBtn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub


                Intent myIntent = new Intent(HomePage.this, MainActivity.class);
                HomePage.this.startActivity(myIntent);

            }
        });
    }   

}

主要活动:

package com.example.clearlight;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
import android.widget.Toast;

import java.net.URL;

import org.apache.http.client.ResponseHandler;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.StrictMode;
import android.util.Log;

public class MainActivity extends Activity {

    TextView txt;

    @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        
        
        setContentView(R.layout.relative);
               
        
        class LongOperation extends AsyncTask<String, Void, String> {
             
            @Override
            protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
           
            String result=null;
            URL url = null;
 
            DefaultHttpClient httpclient = null;
 
            try {
 
            String registrationUrl = "http://10.0.2.2/SensorInfo/GetLightData?sensor=light";
            url = new URL(registrationUrl);
 
            HttpGet getRequest = new HttpGet(registrationUrl);
            ResponseHandler<String> handler = new BasicResponseHandler();
            httpclient = new DefaultHttpClient();
            // request data from server
            result = httpclient.execute(getRequest, handler);
            Log.d("MyApp", "Data from server is "+ result);
             }
            catch (Exception ex) {Log.e("error",ex.toString());
            ex.printStackTrace();
              }
            return result;
            }
 
            @Override
            protected void onPostExecute(String result)
            {              
              TextView text1 = (TextView)MainActivity.this.findViewById(R.id.text);
 
            //Sets the new text to TextView (runtime click event)//*******
            text1.setText("Light Data= " + result);
 
            Toast.makeText(MainActivity.this, "Light Data:" + result, Toast.LENGTH_SHORT).show(); //MESSAGE BOX
              
            }
 
        }
 
 }
}

显现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.clearlight"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.clearlight.MainActivity"
            android:label="@string/app_name" >
            
        </activity>
         <activity
            android:name="com.example.clearlight.HomePage"
            android:label="@string/homepage" 
            android:parentActivityName="com.example.clearlight.MainActivity" >

            <!-- Moved the intent filter to HomePage -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.clearlight.MainActivity" />
        </activity>
    </application>

</manifest>

日志猫:

03-20 01:08:20.433:D/Launcher.Model(418):在配置更改时重新加载应用程序。curr_mcc:310 prevmcc:0 03-20 01:08:20.473:E/StrictMode(401):在附加的堆栈跟踪中获取了资源,但从未释放。有关避免资源泄漏的信息,请参阅 java.io.Closeable。03-20 01:08:20.473:E/StrictMode(401):java.lang.Throwable:未调用显式终止方法“关闭”03-20 01:08:20.473:E/StrictMode(401):在 dalvik.system .CloseGuard.open(CloseGuard.java:184) 03-20 01:08:20.473: E/StrictMode(401): 在 android.database.CursorWindow.(CursorWindow.java:137) 03-20 01:08:20.473: E/StrictMode(401): 在 android.database.CursorWindow.(CursorWindow.java:41) 03-20 01:08:20.473: E/StrictMode(401): 在 android.database.CursorWindow$1.createFromParcel(CursorWindow.java :681) 03-20 01:08:20.473: E/StrictMode(401): 在 android.database。E/StrictMode(401): 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 03-20 01:08:20.473: E/StrictMode(401): 在 java.lang.Thread.run (Thread.java:856) 03-20 01:08:20.484: D/Launcher.Model(418): 为 info=ShortcutInfo(title=Settingsintent=Intent { act=android.intent.action.MAIN 保存图标位图cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.settings/.Settings }id=6 type=0 container=-100 screen=3 cellX=2 cellY=3 spanX=1 spanY=1 isGesture=false dropPos=null) 03-20 01:08:20.534:E/StrictMode(401):在附加的堆栈跟踪中获取了资源,但从未释放。有关避免资源泄漏的信息,请参阅 java.io.Closeable。03-20 01:08:20.534:E/StrictMode(401):java.lang.Throwable:未调用显式终止方法“关闭”03-20 01:08:20.534:E/StrictMode(401):08:23.623:D/dalvikvm(637):DexOpt:加载 310 毫秒,验证 + 选择 1126 毫秒,788740 字节 03-20 01:08:23.833:D/EmailProvider(570):来自数据库帐户:0 03-20 01:08: 23.844: D/EmailProvider(570): 结束 toDatabase 交易;copyCount = 0 03-20 01:08:23.873: D/EmailProvider(570): 结束 fromDatabase 事务;copyCount = 0 03-20 01:08:24.653:I/Email(570):一次性初始化:完成。03-20 01:08:24.964: D/dalvikvm(163): GC_CONCURRENT 释放 452K, 47% 释放 10034K/18887K, 暂停 33ms+15ms, 总共 129ms 03-20 01:08:24.964: D/PackageManager(163): /data/app/com.example.clearlight-1.apk 03-20 01:08:25.804 中安装的新包:I/ActivityManager(163):强制停止包 com.example.clearlight uid=10037 03-20 01: 08:25.954:W/ResourceType(418):尝试检索无效或循环的包 0x7f0d0017。03-20 01:08:25.954: W/ResourceType(418):尝试检索无效或循环的包 0x7f0d0017。03-20 01:08:25.954: W/ResourceType(418): 尝试检索无效或循环的包 0x7f0d0017。03-20 01:08:25.954: W/ResourceType(418): 尝试检索无效或循环的包 0x7f0d0017。03-20 01:08:25.954: W/ResourceType(418): 尝试检索无效或循环的包 0x7f0d0017。03-20 01:08:26.294: D/AlertService(401): 0 Action = android.intent.action.PROVIDER_CHANGED 03-20 01:08:26.294: D/AlertService(401): 开始 updateAlertNotification 03-20 01:08 :26.424: D/dalvikvm(163): WAIT_FOR_CONCURRENT_GC 阻塞 0ms 03-20 01:08:26.554: I/AccountTypeManager(501): 为 1 个帐户类型加载元数据,0 个帐户在 78ms(wall) 5ms(cpu) 03 -20 01:08:26.604:I/Choreographer(418):跳过 1623 帧!应用程序可能在其主线程上做了太多工作。03-20 01:08:26.735: D/PackageManager(163): generateServicesMap(android.accounts.AccountAuthenticator): 2 个服务不变 03-20 01:08:26.754: I/InputReader(163): 重新配置输入设备。更改 = 0x00000010 03-20 01:08:26.764: D/PackageManager(163): generateServicesMap(android.content.SyncAdapter): 4 个服务未更改 03-20 01:08:26.764: D/BackupManagerService(163): 收到广播意图{ act=android.intent.action.PACKAGE_ADDED dat=package:com.example.clearlight flg=0x8000010(有附加功能)} 03-20 01:08:26.984: D/gralloc_goldfish(418): 没有检测到 GPU 仿真的模拟器。03-20 01:08:26.994:V/BackupManagerService(163):addPackageParticipantsLocked:#1 03-20 01:08:27.124:W/InputMethodManagerService(163):在系统 IME 中找不到子类型:com.android.inputmethod。WAIT_FOR_CONCURRENT_GC 阻塞 511 毫秒 03-20 01:08:27.814: D/dalvikvm(163): WAIT_FOR_CONCURRENT_GC 阻塞 444 毫秒 03-20 01:08:27.824: D/dalvikvm(163): WAIT_FOR_CONCURRENT_GC 阻塞 444 毫秒 01:80 27.82 D/dalvikvm(163): WAIT_FOR_CONCURRENT_GC 阻塞 413ms 03-20 01:08:27.834: D/dalvikvm(163): WAIT_FOR_CONCURRENT_GC 阻塞 372ms 03-20 01:08:27.834: D/dalvikvm(163): WAIT_FOR_03-ms_阻塞20 01:08:27.834: D/dalvikvm(163): WAIT_FOR_CONCURRENT_GC 阻塞 369ms 03-20 01:08:27.844: D/dalvikvm(163): WAIT_FOR_CONCURRENT_GC 阻塞 371ms 03-20 01:08:27.864: D/dalvikvm( ): WAIT_FOR_CONCURRENT_GC 阻塞 369ms 03-20 01:08:27.844: D/dalvikvm(163): WAIT_FOR_CONCURRENT_GC 阻塞 327ms 03-20 01:08:27.844: D/dalvikvm(163): WAIT_FOR_CONCURRENT_GC 0 阻塞 324ms :-03 27.944: D/ExchangeService(587): !!! EAS ExchangeService, 13% 免费 19091K/21895K,暂停 20ms+19ms,总计 126ms 03-20 01:08:30.024: D/dalvikvm(277): WAIT_FOR_CONCURRENT_GC 阻塞 107ms 03-20 01:08:30.504: D/AndroidRuntime(250): 关闭down VM 03-20 01:08:30.524: D/dalvikvm(250): GC_CONCURRENT freed 97K, 78% free 453K/2048K, paused 1ms+2ms, total 15ms 03-20 01:08:30.524: D/jdwp(250 ): 得到唤醒信号,退出选择 03-20 01:08:30.524: D/dalvikvm(250): 调试器已分离;对象注册表有 1 个条目 03-20 01:08:30.534:I/AndroidRuntime(250):注意:线程“Binder_3”的附加失败 03-20 01:08:30.794:I/InputReader(163):重新配置输入设备。更改 = 0x00000010 03-20 01:08:30.844: D/PackageManager(163): generateServicesMap(android.accounts.AccountAuthenticator): 2 服务未更改 03-20 01:08:30.864: I/InputReader(163): 重新配置输入设备. 尝试加载 lib libjavacore.so 0x0 03-20 01:08:31.664:D/dalvikvm(668):添加了共享库 libjavacore.so 0x0 03-20 01:08:31.734:D/dalvikvm(668):尝试加载lib libnativehelper.so 0x0 03-20 01:08:31.734:D/dalvikvm(668):添加共享库 libnativehelper.so 0x0 03-20 01:08:31.904:D/dalvikvm(163):GC_CONCURRENT 释放 404K,48% free 10005K/18887K, paused 116ms+20ms, total 388ms 03-20 01:08:32.254: D/dalvikvm(501): GC_CONCURRENT freed 203K, 4% free 8205K/8519K, paused 98ms+21ms, total 963ms 03-20 01 :08:32.564: D/AlertService(401): 没有触发或预定警报 03-20 01:08:32.834: D/dalvikvm(469): GC_CONCURRENT 释放 232K,4% 释放 8181K/8519K,暂停 90ms+7ms,总计540 毫秒 03-20 01:08:33.074:I/Choreographer(418):跳过 1563 帧!应用程序可能在其主线程上做了太多工作。03-20 01:08:33.094: D/AndroidRuntime(668): D / dalvikvm(682):未启用CheckJNI(已启用)03-20 01:08:33.635:I / ActivityManager(163):为活动com.example.clearlight / .HomePage启动proc com.example.clearlight: pid=682 uid=10037 gids={3003, 1028} 03-20 01:08:34.093: E/Trace(682): 打开跟踪文件时出错:没有这样的文件或目录 (2) 03-20 01:08:34.314 : I/ARMAssembler(35): 生成扫描线__00000077:03515104_00009001_00000000 [131 ipp] (153 ins) at [0x4124a8d0:0x4124ab34] in 736260 ns 03-20 01:08:34.754: D/gralloc_emulator 没有检测到 GPUgoldfish 682 . 03-20 01:08:34.824: I/ActivityManager(163): 显示 com.example.clearlight/.HomePage: +1s253ms 03-20 01:08:35.484: I/ContactDirectoryManager(375): 在 7915ms 内发现 0 个联系人目录03-20 01:08:35.484: I/Launcher(418): setLoadOnResume 03-20 01:08:35.564: I/Launcher(418): setLoadOnResume 03-20 01: 应用程序可能在其主线程上做了太多工作。03-20 01:08:37.373: E/WVMExtractor(38): 未能打开 libwvm.so 03-20 01:08:38.434: I/Launcher(418): setLoadOnResume 03-20 01:08:38.454: I/ Launcher(418): setLoadOnResume 03-20 01:08:38.543: E/WVMExtractor(38): 未能打开 libwvm.so 03-20 01:08:38.603: E/MetadataRetrieverClient(38): 无法提取专辑封面03-20 01:08:39.003: D/dalvikvm(375): GC_CONCURRENT 释放 387K, 6% 释放 8414K/8903K, 暂停 31ms+5ms, 总共 110ms 03-20 01:08:39.263: I/ContactsDatabaseHelper(375):语言环境更改在 3621 毫秒内完成 03-20 01:08:39.263:I/ContactsDatabaseHelper(375):切换到语言环境 en_US 03-20 01:08:39.763:I/ContactsDatabaseHelper(375):语言环境更改在 501 毫秒内完成 03-20 01 :08:39.773: I/ContactsProvider(375): 升级聚合算法 03-20 01:08:39.803:

4

0 回答 0