1

我创建了一个从 web 服务下载 xml 并执行一些业务逻辑的应用程序。我已经完成了应用程序并在模拟器上成功测试,效果很好。

但是当我安装在使用 gprs 运行互联网的真实设备上时,每次只有一部分 xml 被下载,所以我的应用程序无法运行。

这是代码登录

class LoginTask extends AsyncTask<String, Void, String> {

        String usr;
        String passwd;

        @Override
        protected void onPreExecute() {
            // // User entered data
            usr = text_username.getText().toString();
            passwd = text_password.getText().toString();
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... url) {
            try {
                String xml = getXmlFromUrl(mypref.getNavURL());
                if (xml == null) {

                    return null;
                }
                LoginResponse login = NavizenXmlParserFn.getLoginResponse(xml);
                // Set session
                Session mSession = Session.getSession();
                mSession.setUserId(usr);
                if (login.getStatus().equals(ResponseCodes.LOGIN_SUCCESS)) {
                    List<WorkCenterResponse> workcenter = NavizenXmlParserFn
                            .getWorkCenterResponse(xml);
                    NavisionApplication app = (NavisionApplication) getApplication();
                    app.setWorkCenterList(workcenter);
                    return ResponseCodes.LOGIN_SUCCESS;
                } else if (login.getStatus()
                        .equals(ResponseCodes.LOGIN_FAILURE)) {
                    return ResponseCodes.LOGIN_FAILURE;
                }

            } catch (Exception e) {
                e.printStackTrace();
                LogTofile.writeException(e);
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            showToast(response);
            pd_login.dismiss();
            // if (validateLocally().equals(ResponseCodes.LOGIN_SUCCESS)) {
            // Intent intent = new Intent(Login.this, SelectCenter.class);
            // startActivity(intent);
            // } else {
            // showDialogOk("Error", "Invalid username or password");
            // }
            if (result == null) {
                showDialogOk("Unable to connect", "Try Again later");
//              Intent intent = new Intent(Login.this, SelectCenter.class);
//              startActivity(intent);
                return;
            }
            if (result.equals(ResponseCodes.LOGIN_SUCCESS)) {
                Intent intent = new Intent(Login.this, SelectCenter.class);
                startActivity(intent);
            } else {
                showDialogOk("Error", "Invalid username or password");
            }

        }
        String response;
        public String getXmlFromUrl(String url) {

            try {
                LoginRequest loginRequest = new LoginRequest();
                loginRequest.mPassword = passwd;
                loginRequest.mRequestDate = DateUtility.getDate();// "06-20-2011";
                loginRequest.mRequestEntity = "Login";
                loginRequest.mRequestID = Constants.mRequestID;
                loginRequest.mRequestTime = DateUtility.getTime();// "19:03:22";
                loginRequest.mRequestType = "New";
                loginRequest.mTransactionID = Constants.mTransactionID;
                loginRequest.mUserID = usr;

                final String xml = NavizenXmlParserFn
                        .getRequestxml(loginRequest);
                Log.i("LoginRequest", xml);

                response = NavConnection.connection(url,
                        xml, mypref.getNavUserId(), mypref.getNavPassword());
                LogTofile.writeString("Login Response: \n"+response);
                Log.i("LoginrResponse", response); //when only part of the xml gets printed


                return response;

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                LogTofile.writeException(e);
                return null;
            } catch (ClientProtocolException e) {
                e.printStackTrace();
                LogTofile.writeException(e);
                return null;
            } catch (IOException e) {
                e.printStackTrace();
                LogTofile.writeException(e);
                return null;
            } catch (Exception e) {
                LogTofile.writeException(e);
                e.printStackTrace();
                return null;
            }

        }

    }

这是 xml 的一部分

在此处输入图像描述

这是连接代码

public static String connection(String url, String xml, String id,
        String pass) {
    String xml2 ;
    try {
        HttpParams httpParameters = new BasicHttpParams();
        int timeConnectionOut = 60000;
        HttpConnectionParams.setConnectionTimeout(httpParameters,
                timeConnectionOut);
        int timeoutSocket = 60000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
        Log.i("url passon server", " " + url);

        StringEntity se = new StringEntity(xml, HTTP.UTF_8);
        se.setContentType("text/xml");

        HttpPost httpPost = new HttpPost(url);

        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("reqxml", xml));
        nameValuePairs.add(new BasicNameValuePair("user_id", id));
        nameValuePairs.add(new BasicNameValuePair("u_password", pass));

        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();

        xml2 = EntityUtils.toString(httpEntity);
        //Log.e("Connection  Response:", xml2);


    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        //showToast(e.getMessage());
        Log.e("Connection  :", "UnsupportedEncodingException exception : "
                + e.getMessage());
        return null;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        //showToast(e.getMessage());
        Log.e("Connection :",
                "ClientProtocolException exception : " + e.getMessage());
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        //showToast(e.getMessage());
        LogTofile.writeException(e);
        Log.e("Connection :", "IOException exception : " + e.getMessage());
        return null;
    } catch (RuntimeException e) {
        e.printStackTrace();
        //showToast(e.getMessage());
        Log.e("Connection :",
                "RuntimeException exception : " + e.getMessage());
        return null;
    }
    return xml2;
}
4

1 回答 1

0

您是否尝试打印日志并查看您的连接是否在两者之间没有中断?从服务器获取数据的应用程序的核心代码似乎在以下行中:

   response = NavConnection.connection(url, xml, mypref.getNavUserId(), mypref.getNavPassword());

因此,您必须在该类中打印一些日志语句并查看发生了什么。尝试使用一些连接参数等。

于 2012-10-21T15:19:18.550 回答