私は Android プログラミングが初めてで、サーブレットを介してデータベースからデータを収集するアプリを作成しようとしています。Eclipse の再インストールを余儀なくされるまで、アプリは機能していました。現在、サーブレットからデータを取得する際に問題が発生しているようです。アプリは準拠して実行されますが、「軽いデータ」は表示されません。AsyncTask は明らかにこの問題に役立つ可能性があります (ただし、単純なアプリの入力ボタンを備えたホームページを作成し、それを新しいメイン アクティビティとして設定しました)。AsyncTask を使用して、自分のコードに固有の回答をいただければ幸いです。
サーブレットに接続するコード: このページは、ホームページ (新しいメイン アクティビティ) ボタンから入力した後に表示されます。
package com.example.clearlight;
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);
// Create a crude view - this should really be set via the layout resources but since its an example saves declaring them in the XML.
/*LinearLayout rootLayout = new LinearLayout(getApplicationContext());
txt = new TextView(getApplicationContext());
rootLayout.addView(txt);
setContentView(rootLayout);*/
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
String result = httpclient.execute(getRequest, handler);
Log.d("MyApp", "Data from server is "+ result);
//Creating TextView Variable**********************************
TextView text1 = (TextView) findViewById(R.id.text);
//Sets the new text to TextView (runtime click event)//*******
text1.setText("Light Data= " + result);
Toast.makeText(this, "Light Data:" + result, Toast.LENGTH_SHORT).show(); //MESSAGE BOX
//txtMessage.setText(String.valueOf(msg1) + " " + String.valueOf(msg2));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
マニフェスト:
<?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>