我有一个Android应用程序,它包含一个活动和一个扩展AsyncTask的java类。所以我在java类中有一个名为 resFromServer 的String 我希望在一个合适的Android小部件中显示它,如Alertdialog。
我希望有人帮助我如何从活动中访问该字符串
public class JSONTransmitter extends AsyncTask<JSONObject, JSONObject, JSONObject> {
String url = "http://192.168.1.8:89/Derdeery/b.php";
protected JSONObject doInBackground(JSONObject... data) {
JSONObject json = data[0];
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
JSONObject jsonResponse = null;
HttpPost post = new HttpPost(url);
try {
StringEntity se = new StringEntity("json="+json.toString());
post.addHeader("content-type", "application/x-www-form-urlencoded");
post.setEntity(se);
HttpResponse response;
response = client.execute(post);
String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity());
Log.i("Response from server", **resFromServer**);
}
catch (Exception e)
{ e.printStackTrace();}
return jsonResponse;
}
}
活动
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} }
答案 0 :(得分:2)
正确的解决方案:创建instance
的{{1}}并进行回调,以便将响应捕获到任何AsyncTask
中。将实现此接口的所有活动都可以检索AsyncTask的结果。这更灵活,只需一个AsyncTask即可由多个活动使用。
您必须在Activity
中创建实例并返回String
响应:
AsyncTask
然后,您可以通过将public class JSONTransmitter extends AsyncTask<JSONObject, Void, String> {
private AsyncCallback asyncCallback; // callback variable
// constructor
public JSONTransmitter(Context context) {
// attach the callback to any context
asyncCallback = (AsyncCallback) context;
}
// create the interface
public static interface AsyncCallback {
void onResponse(String res);
}
...
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (asyncCallback != null) {
// call the onResponse method for callback
asyncCallback.onResponse(result);
}
}
}
设置为Activity
来实现任何Context
回调:
public class MainActivity extends Activity implements JSONTransmitter.AsyncCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// set the Context to the AsyncTask when you create it
// in order to return here and execute the request
new JSONTransmitter(this).execute(...);
}
...
@Override
public void onResponse(String res) {
// handle the string response when it's called from onPostExecute()
Log.v("From_MainActivity", "AsyncTask returns this: "+res);
}
}
因此,您可以使用Activity
类resFromServer
AsyncTask
(结果字符串)在self
中执行您想要的操作。
答案 1 :(得分:0)
将您的活动作为参数传递给AsyncTask
类构造函数:
class MyAsyncTask extends AsyncTask<URL, Integer, Long> {
Activity mActivity;
public MyAsyncTask(Activity activity) {
mActivity = ativity;
}
然后更新您的活动:
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
int id = mActivity.findViewById(...);
}
您不应使用doInBackground
方法更新活动。
使用onPostExecute
,因为它使用主线程。