【发布时间】:2021-02-22 10:11:35
【问题描述】:
我正在尝试读取返回 0 或 -1 的 uri。 从 Fragment 内部调用 asynctask。 基本上,当用户单击一个按钮时,它的登录是在远程服务器上检查输入。
在调试器中,任务显示为 RUNNING 但从未完成,也没有调用被覆盖的成员,这可以解释为什么它什么都不做。
登录密码是:
// Validate the user.
String uri = HLConsts.SERV_URI + "ValidateLoginUser?uname=" + szUserName + "&password=" + szPassword + "&android=1";
ReadUri rd = new ReadUri(uri);
int cnt =0;
rd.execute();
while ( rd.getSzReturn() == null || cnt > 5) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
cnt++;
}
if(cnt > 5) {
errors++;
}
String szValid = rd.getSzReturn();
Snackbar.make(root, "Name = " + szUserName + " Pass = " + szPassword , Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
============================= 异步任务是:
public class ReadUri extends AsyncTask< Void , Void, String> {
// private Context mContext;
private String mUrl;
private String szReturn = null;
public String getSzReturn() {
return szReturn;
}
public ReadUri( String url) {
// mContext = context;
mUrl = url;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// dynamictext = (TextView) myView.findViewById(R.id.textview_first);
}
@Override
protected String doInBackground(Void... params) {
String resultString = null;
resultString = getString(mUrl);
return resultString;
}
@Override
protected void onPostExecute(String strings) {
super.onPostExecute(strings);
szReturn =strings;
}
//======================================================================================
private String getString(String InUrl) {
String ret = "Nothing";
URL url = null;
try {
url = new URL(InUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
// The line below is where the exception is called
int response = 0;
try {
response = httpURLConnection.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("Response", String.valueOf(response));
if (response == HttpURLConnection.HTTP_OK) {
// Response successful
InputStream inputStream = null;
try {
inputStream = httpURLConnection.getInputStream();
} catch (IOException ex) {
ex.printStackTrace();
}
// Parse it line by line
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
StringBuffer sb = new StringBuffer();
while (true) {
try {
// if (!((temp = bufferedReader.readLine()) != null)) {
while((line=bufferedReader.readLine())!=null)
{
// sb.append(line); //appends line to string buffer
// sb.append("\n"); //line feed
ret += line;
Log.w("MyApp",line);
}
} catch (IOException ex) {
ex.printStackTrace();
}
// Parsing of data here
}
} else {
Log.e("SAMES", "INCORRECT RETURN CODE: " + response);
}
httpURLConnection.disconnect();
// urlConnection.disconnect();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return ret;
}
}
请你帮忙。我设置了断点,没有一个被触发,doInBackground 也没有被调用。
谢谢 杰夫。
【问题讨论】:
-
rd.execute();删除该语句之后的所有代码行。在 onPostExecute 中处理结果。 -
|| cnt > 5)我想你提到了|| cnt < 5)。但无论如何,这是糟糕的编码。关注我的第一条评论。
标签: android android-asynctask android-fragmentactivity