笛里谁知壮士心,沙头空照征人骨。这篇文章主要讲述在Android中如何从Asynctask接收字符串?相关的知识,希望能为你提供帮助。
在我的android项目中,我在MainActivity
内:
String receivedData = https://www.songbingjia.com/android/new myTask().execute().get();
现在
myTask
任务是:public class MainActivity extends Activity {
private Button button;
private TextView finalResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn_do_it);
finalResult = (TextView) findViewById(R.id.tv_result);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String receivedData = https://www.songbingjia.com/android/new myTask().execute().get();
finalResult.setText(receivedData);
}
});
}private class myTask extends AsyncTask<
String, Void, String>
{
@Override
protected Void doInBackground(Void... params) {
Gson gson = new Gson();
List<
Myobject>
newObject = new ArrayList<
Myobject>
();
try {
//here I add object valus to the list
newObject.add(valueadded)
} catch {
Exception e
}
//here returning the value as String
return gson.toJson(newObject);
}protected void onPostExecute(String result) {
String receivedData = result;
}
}
}
但是receiveData没有收到
AsyncTask
的任何价值。为什么会这样?我究竟做错了什么?答案您的AsyncTask定义如下:
private class myTask extends AsyncTask<
String, Void, Void>
最后一个泛型类型是结果的类型。你有空虚。这意味着您的AsyncTask不返回值。如果你想要一个String结果,你必须像这样定义它:
private class myTask extends AsyncTask<
String, Void, String>
这意味着您现在可以从doInBackground()返回一个String。
另一答案尝试这样的事情
public class MainActivity extends Activity {
String receivedData;
private Button button;
private TextView finalResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn_do_it);
finalResult = (TextView) findViewById(R.id.tv_result);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new myTask().execute().get();
}
});
}private class myTask extends AsyncTask<
String, Void, String>
{
@Override
protected String doInBackground(Void... params) {
Gson gson = new Gson();
List<
Myobject>
newObject = new ArrayList<
Myobject>
();
try {
//here I add object valus to the list
newObject.add(valueadded)
} catch (Exception e) {
//here returning the value as String
return gson.toJson(newObject);
}
}protected void onPostExecute(String result) {
String receivedData = https://www.songbingjia.com/android/result;
finalResult.setText(receivedData);
}
}
}
另一答案【在Android中如何从Asynctask接收字符串()】您可以在Async Task中添加onPostExecute方法以接收String。喜欢下面
private class myTask extends AsyncTask<
String, Void, String>
{
@Override
protected Void doInBackground(Void... params) {
.........
return someString;
}
protected void onPostExecute(String result) {
String receivedData = https://www.songbingjia.com/android/result;
}
}
推荐阅读
- 为什么我用AsyncTask获取android.os.NetworkOnMainThreadException()
- Android SDK AsyncTask doInBackground未运行(子类)
- Android Media Player(开始调用状态4错误(-38,0))
- Android AsyncTask在完成工作后返回值
- 如何解决MySQL异常SQLSTATE [HY000](常规错误:1364字段”field_name”没有默认值)
- 如何解决(解决方案)Google的”程序员”游戏(海龟级)
- 如何在CentOS中安装bcmath扩展
- 如何使用EventListener根据用户在Symfony 2.8中的角色将用户重定向到特定页面
- 如何在Symfony 3.4中使用MonologBu??ndle通过电子邮件自动报告异常