Return 来自 Asynctask 的字符串
Return string from Asynctask
我正在尝试解析一个 json 对象,但我无法将 doInBackground 的结果转换为 onPostExecute
这是我的 AsyncTask 代码:
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MyActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
String auth2 = jsonObj.getString("auth");
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void auth2) {
super.onPostExecute(auth2);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
Toast.makeText(getApplicationContext(), "String retrived:" + auth2, Toast.LENGTH_SHORT).show();
}
}
我知道它可能是因为我在那里有 return null,但是当我制作 return 字符串时我得到错误。
我知道 jsonStr 保存 json 数据,我可以在日志中看到它:
回应:﹕ > {"user_info":{"auth":0}}
我从教程中整理了这段代码,这就是为什么我不完全理解它的原因。
我的目标是查看 auth 是 0 还是 1。
阅读文档:
http://developer.android.com/reference/android/os/AsyncTask.html
private class GetContacts extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... arg0) {
...
return "askdjalskdj";
}
@Override
protected void onPostExecute(String auth2) {
Log.i("Output", auth2);
}
}
查看我在 Asynctask 的通用实现中设置的参数,查看 doInBackground 中定义的 return 值和 onPostExecute 的参数类型
AsyncTask's generic types The three types used by an asynchronous task are the following:
- Params, the type of the parameters sent to the task upon execution.
- Progress, the type of the progress units published during the
background computation.
- Result, the type of the result of the
background computation.
Not all types are always used by an
asynchronous task. To mark a type as unused, simply use the type Void:
private class MyTask extends AsyncTask { ... }
cant get the result out of doInBackground into onPostExecute
至 return auth2
来自 doInBackground
的字符串:
1. 将 doInBackground
方法的 return 类型从 Void
更改为 String
:
@Override
protected String doInBackground(Void... arg0) {
}
2. 将 AsyncTask
最后一个通用类型从 Void
更改为 String
:
private class GetContacts extends AsyncTask<Void, Void, String>
3. Return auth2
来自 doInBackground :
String auth2 = jsonObj.getString("auth");
return auth2;
4. 将 onPostExecute
参数类型从 Void
更改为 String
:
@Override
protected void onPostExecute(String auth2) {
super.onPostExecute(auth2);
//...
Toast.makeText(getApplicationContext(),
"String retrived:" + auth2, Toast.LENGTH_SHORT).show();
}
我正在尝试解析一个 json 对象,但我无法将 doInBackground 的结果转换为 onPostExecute
这是我的 AsyncTask 代码:
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MyActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
String auth2 = jsonObj.getString("auth");
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void auth2) {
super.onPostExecute(auth2);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
Toast.makeText(getApplicationContext(), "String retrived:" + auth2, Toast.LENGTH_SHORT).show();
}
}
我知道它可能是因为我在那里有 return null,但是当我制作 return 字符串时我得到错误。
我知道 jsonStr 保存 json 数据,我可以在日志中看到它: 回应:﹕ > {"user_info":{"auth":0}}
我从教程中整理了这段代码,这就是为什么我不完全理解它的原因。
我的目标是查看 auth 是 0 还是 1。
阅读文档: http://developer.android.com/reference/android/os/AsyncTask.html
private class GetContacts extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... arg0) {
...
return "askdjalskdj";
}
@Override
protected void onPostExecute(String auth2) {
Log.i("Output", auth2);
}
}
查看我在 Asynctask 的通用实现中设置的参数,查看 doInBackground 中定义的 return 值和 onPostExecute 的参数类型
AsyncTask's generic types The three types used by an asynchronous task are the following:
- Params, the type of the parameters sent to the task upon execution.
- Progress, the type of the progress units published during the background computation.
- Result, the type of the result of the background computation.
Not all types are always used by an asynchronous task. To mark a type as unused, simply use the type Void:
private class MyTask extends AsyncTask { ... }
cant get the result out of doInBackground into onPostExecute
至 return auth2
来自 doInBackground
的字符串:
1. 将 doInBackground
方法的 return 类型从 Void
更改为 String
:
@Override
protected String doInBackground(Void... arg0) {
}
2. 将 AsyncTask
最后一个通用类型从 Void
更改为 String
:
private class GetContacts extends AsyncTask<Void, Void, String>
3. Return auth2
来自 doInBackground :
String auth2 = jsonObj.getString("auth");
return auth2;
4. 将 onPostExecute
参数类型从 Void
更改为 String
:
@Override
protected void onPostExecute(String auth2) {
super.onPostExecute(auth2);
//...
Toast.makeText(getApplicationContext(),
"String retrived:" + auth2, Toast.LENGTH_SHORT).show();
}