関数の1つがAndroidデバイスから外部SQLデータベースのユーザープロファイルを更新することであるアプリケーションを作成しています。したがって、バックグラウンド非同期タスク スレッドを使用して update.php ファイルにリクエストを送信しています。アプリケーションはエミュレータ 2.3.3 で正常に動作し、データベース内のプロファイルを正常に更新しますが、Android 4.0.3 を搭載したタブレットまたは電話で実行しようとすると、onPreExecute() で定義された ProgressDialog が終了するまで長い時間がかかります。エラーが発生します。ここにスタックがあります:
08-12 08:46:14.303: E/Buffer Error(3167): Error converting result java.io.IOException: Attempted read on closed stream.
08-12 08:46:14.303: D/Update Response(3167): {"student":[{"understandPref":"0","genderPref":"Male","username":"ma","secondLangPref":"none","bilingualismPref":"no","ethnicPref":"American","lichPref":"0","pobPref":"Argentina","password":"fd","speakPref":"0","domLangPref":"US\/English","agePref":"15"}],"success":1}
08-12 08:46:14.303: D/AndroidRuntime(3167): Shutting down VM
08-12 08:46:14.303: W/dalvikvm(3167): threadid=1: thread exiting with uncaught exception (group=0x409e21f8)
08-12 08:46:14.323: E/AndroidRuntime(3167): FATAL EXCEPTION: main
08-12 08:46:14.323: E/AndroidRuntime(3167): java.lang.IllegalArgumentException: View not attached to window manager
08-12 08:46:14.323: E/AndroidRuntime(3167): at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:587)
08-12 08:46:14.323: E/AndroidRuntime(3167): at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:324)
08-12 08:46:14.323: E/AndroidRuntime(3167): at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:151)
08-12 08:46:14.323: E/AndroidRuntime(3167): at android.app.Dialog.dismissDialog(Dialog.java:328)
08-12 08:46:14.323: E/AndroidRuntime(3167): at android.app.Dialog$1.run(Dialog.java:119)
08-12 08:46:14.323: E/AndroidRuntime(3167): at android.app.Dialog.dismiss(Dialog.java:313)
08-12 08:46:14.323: E/AndroidRuntime(3167): at edu.neiu.voiceofchicago.activities.SettingsActivity$UpdateAccount.onPostExecute(SettingsActivity.java:519)
08-12 08:46:14.323: E/AndroidRuntime(3167): at edu.neiu.voiceofchicago.activities.SettingsActivity$UpdateAccount.onPostExecute(SettingsActivity.java:1)
08-12 08:46:14.323: E/AndroidRuntime(3167): at android.os.AsyncTask.finish(AsyncTask.java:602)
08-12 08:46:14.323: E/AndroidRuntime(3167): at android.os.AsyncTask.access$600(AsyncTask.java:156)
08-12 08:46:14.323: E/AndroidRuntime(3167): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
08-12 08:46:14.323: E/AndroidRuntime(3167): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 08:46:14.323: E/AndroidRuntime(3167): at android.os.Looper.loop(Looper.java:137)
08-12 08:46:14.323: E/AndroidRuntime(3167): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-12 08:46:14.323: E/AndroidRuntime(3167): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 08:46:14.323: E/AndroidRuntime(3167): at java.lang.reflect.Method.invoke(Method.java:511)
08-12 08:46:14.323: E/AndroidRuntime(3167): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-12 08:46:14.323: E/AndroidRuntime(3167): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-12 08:46:14.323: E/AndroidRuntime(3167): at dalvik.system.NativeStart.main(Native Method)
なぜエミュレーターで動作し、電話では動作しないのですか? お知らせ下さい。ありがとう。
JSONParser.class
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) throws IOException {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception ex)
{
Log.d("Networking", ex.getLocalizedMessage());
throw new IOException("Error connecting");
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}