2012-10-21 26 views
1

我创建了一个应用程序,它从webservice下载xml并执行一些业务逻辑。 我已经完成了应用程序,并在仿真器上成功测试完美。只有部分xml被下载到真实设备上android

但当我在使用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

enter image description here

这里的这个部分是用于连接

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; 
} 

回答

0

你尝试和打印的代码记录并查看您的连接是否在两者之间无法断开N +看来,从服务器获取数据,应用程序的核心代码是在该行:

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

所以,你必须打印该类一些日志语句,看看发生了什么。尝试玩一些连接参数等

+0

我已经更新了我的连接代码....和需要下载的xml文件只有2KB的大小....我认为连接分解问题将通过http类...应该通过一些例外....但在我的情况下,部分文件越来越混乱 – kadhirvel

+0

你总是得到这部分的XML?或者您每次收到一个不同大小的xml文件。 –

+0

嗨@Sumeet Khullar我检查了我的应用程序在移动上的3G互联网连接....它工作成功....实际问题是我的2G数据连接 – kadhirvel