应用程序在模拟器上运行良好,但在手机上运行时崩溃

时间:2012-08-11 18:23:28

标签: android android-emulator android-asynctask

我正在编写一个应用程序,其中一个功能是从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;

    }
}

1 个答案:

答案 0 :(得分:0)

你的问题是

  

08-12 08:46:14.303:E / Buffer Error(3167):转换结果时出错   java.io.IOException:尝试在封闭流上读取。

将您的阅读内容放在try catch块中。很可能是你关闭了php文件的连接。