Android 无法在已签名的apk中读取webservice
签署apk后,代码不会从web服务读取。我尝试调试,发现BufferedReader没有读取任何行。根据其他stackoverflow问题中的一些答案,我也关闭了Progaud,但仍然不好。这只发生在已签名的apk中Android 无法在已签名的apk中读取webservice,android,Android,签署apk后,代码不会从web服务读取。我尝试调试,发现BufferedReader没有读取任何行。根据其他stackoverflow问题中的一些答案,我也关闭了Progaud,但仍然不好。这只发生在已签名的apk中 public class LoanRatesWS extends AsyncTask<String, String, ArrayList<LoanRatesBn>> { HttpURLConnection connection = null;
public class LoanRatesWS extends AsyncTask<String, String, ArrayList<LoanRatesBn>> {
HttpURLConnection connection = null;
BufferedReader reader = null;
InputStream in;
String line;
StringBuffer buffer = new StringBuffer();
ProgressDialog progressDialog;
int httpStatus;
boolean isTimeout = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
Resources resources = getResources();
String loadingMessage = resources.getString(R.string.loadingMessage);
Utilities.startProgressDialog(progressDialog, loadingMessage);
}
@Override
protected ArrayList<LoanRatesBn> doInBackground(String... params) {
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(Utilities.getConnTimeout(MainActivity.this));
connection.connect();
httpStatus = connection.getResponseCode();
in = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = parentObject.getJSONArray("Loans");
ArrayList<LoanRatesBn> loanRatesBnArrayList = new ArrayList<>();
Gson gson = new Gson();
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
LoanRatesBn loanRatesBn = gson.fromJson(finalObject.toString(), LoanRatesBn.class);
loanRatesBnArrayList.add(loanRatesBn);
}
return loanRatesBnArrayList;
} catch (SocketTimeoutException ex) {
Utilities.stopProgressDialog(progressDialog);
isTimeout = true;
} catch (Exception ex) {
Utilities.stopProgressDialog(progressDialog);
} finally {
if (connection != null) {
connection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(ArrayList<LoanRatesBn> result) {
super.onPostExecute(result);
if (isTimeout) {
Utilities.timeoutErrorAlert(MainActivity.this);
} else if (Utilities.isValidResponse(httpStatus)) {
Utilities.stopProgressDialog(progressDialog);
Intent intent = new Intent(MainActivity.this, LoanRates.class);
Resources res = MainActivity.this.getResources();
intent.putExtra("pageName", res.getString(R.string.loanRatesStr));
intent.putExtra("loanRatesBnArrayList", result);
startActivity(intent);
} else {
Utilities.serverErrorAlert(MainActivity.this);
}
}
}
公共类LoanRatesWS扩展异步任务{
HttpURLConnection=null;
BufferedReader reader=null;
输入流输入;
弦线;
StringBuffer=新的StringBuffer();
进行对话进行对话;
int-httpStatus;
布尔值isTimeout=false;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
progressDialog=新建progressDialog(MainActivity.this);
Resources=getResources();
String loadingMessage=resources.getString(R.String.loadingMessage);
startProgressDialog(progressDialog,loadingMessage);
}
@凌驾
受保护的ArrayList doInBackground(字符串…参数){
试一试{
URL=新URL(参数[0]);
connection=(HttpURLConnection)url.openConnection();
setConnectTimeout(Utilities.getConnTimeout(MainActivity.this));
connection.connect();
httpStatus=connection.getResponseCode();
in=connection.getInputStream();
reader=新的BufferedReader(新的InputStreamReader(in));
而((line=reader.readLine())!=null){
buffer.append(行);
}
字符串finalJson=buffer.toString();
JSONObject parentObject=新的JSONObject(finalJson);
JSONArray parentArray=parentObject.getJSONArray(“贷款”);
ArrayList loanRatesBnArrayList=新ArrayList();
Gson Gson=新的Gson();
对于(int i=0;i
Logcat中是否存在异常?或者应用程序只是等待Web服务的响应?你应该调试你的代码,看看它是否挂起/挂起在哪里。在doInBackground
中添加一些Log.d
调用,以某种方式跟踪它……不,代码挂起不在哪里。甚至HTTP状态也是200。但是logcat中也有例外,但这是由于后续对象保持空。在这些行中发生了一些错误:DoinBackground的2-9个,所以添加Log.d
调用来缩小范围