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