[英]AsyncTask Cant set value in Global variable
以下是我尝试实现AsyncTask
函数。 我正在尝试在全局变量中设置值,因此我可以在任何地方访问它,但无法设置它。
private void doLogIn() {
class SendPostReqAsyncTask extends AsyncTask<Void, Void, String> {
ProgressDialog dialog;
// Show Progress bar before downloading Music
@Override
protected void onPreExecute() {
super.onPreExecute();
// Shows Progress Bar Dialog and then call doInBackground method
dialog = ProgressDialog.show(SignInActivity.this, "", "Loading. Please wait...", true);
}
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
// In a POST request, we don't pass the values in the URL.
//Therefore we use only the web page URL as the parameter of the HttpPost argument
HttpPost httpPost = new HttpPost(MyAppUtil.API_URL + "auth/signIn");
//Now we put those sending details to an ArrayList with type safe of NameValuePair
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
for (String key : inputValList.keySet()) {
BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, inputValList.get(key).toString());
nameValuePairList.add(basicNameValuePair);
}
try {
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
// setEntity() hands the entity (here it is urlEncodedFormEntity) to the request.
httpPost.setEntity(urlEncodedFormEntity);
try {
// HttpResponse is an interface just like HttpPost.
//Therefore we can't initialize them
HttpResponse httpResponse = httpClient.execute(httpPost);
// According to the JAVA API, InputStream constructor do nothing.
//So we can't initialize InputStream although it is not an interface
InputStream inputStream = httpResponse.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
stringBuilder.append(bufferedStrChunk);
}
return stringBuilder.toString();
} catch (ClientProtocolException cpe) {
System.out.println("First Exception caz of HttpResponese :" + cpe);
cpe.printStackTrace();
} catch (IOException ioe) {
System.out.println("Second Exception caz of HttpResponse :" + ioe);
ioe.printStackTrace();
}
} catch (UnsupportedEncodingException uee) {
System.out.println("An Exception given because of UrlEncodedFormEntity argument :" + uee);
uee.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//close process dialog
if (this.dialog != null) {
this.dialog.dismiss();
}
System.out.println(result);
try {
JsonObjProfile = new JSONObject(result);
JSONObject user = JsonObjProfile.getJSONObject("postDataSet");
String id = user.getString("id");
MyAppUtil.user_id = id;
} catch (Exception e) {
e.printStackTrace();
}
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute();
}
这里MyAppUtil.user_id = id;
是静态类的变量,它将用于从任何地方访问值。
静态变量是一个可怕的想法。 例如,如果Android的任务管理器杀死了您的应用程序然后重新启动它,则您的价值将丢失。 改用共享首选项 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.