应用程序在模拟器上运行良好,但在手机上运行时崩溃
问题描述:
我正在编写一个应用程序,其中一个功能是从android设备更新外部sql数据库中的用户配置文件。所以我使用Background Async任务线程向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;
}
}
答
您的问题是
8月8日至12日:46:14.303:E /缓存器的错误(3167):错误转换结果 java.io.IOException的:试图在封闭流中阅读。
把你的阅读从流部分放在try catch块中。最有可能的是你关闭了php文件的连接。
+0
感谢您的答复。但它仍然不起作用。我已经附上全班。 – 2012-08-15 04:34:42
你可以发布你的代码片段 – 2012-08-13 00:04:50