0

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

    }
}
4

1 回答 1

0

你的问题是

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

将您从流部分读取的内容放入 try catch 块中。很可能您与 php 文件的连接已关闭。

于 2012-08-13T00:04:29.300 回答