我已经使用AsyncTask几天了。我的旧代码在带有runOnUIThred的API 8上工作,它不会在> API 18上工作。我想在doInBackground()中使用JSON将数据发送到DB,并在onPostExecute()中获取数据。如何将代码分成两部分?
class GetProductDetails extends AsyncTask<String, String, Integer> {
@Override
protected void onPreExecute() {
//bla-bla-bla
}
protected Integer doInBackground(String... args) {
int success = 0;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("pid", pid));
JSONObject json = jsonParser.makeHttpRequest(
url_product_detials, "GET", params);
success = json.getInt(TAG_SUCCESS);
} catch (Exception e) {
e.printStackTrace();
}
return success;
}
protected void onPostExecute(Integer success) {
pDialog.dismiss();
JSONArray productObj = json.getJSONArray(TAG_PRODUCT);
JSONObject product = productObj.getJSONObject(0);
if (success == 1) {
txtName = (TextView) findViewById(R.id.tvName);
txtPrice = (TextView) findViewById(R.id.tvPrice);
txtDesc = (TextView) findViewById(R.id.tvDescription);
txtCurrent = (TextView) findViewById(R.id.tvCurrent);
txtTurn = (TextView) findViewById(R.id.tvTurn);
txtName.setText(product.getString(TAG_NAME));
txtPrice.setText(product.getString(TAG_PRICE));
txtDesc.setText(product.getString(TAG_DESCRIPTION));
txtCurrent.setText(product.getString("current"));
txtTurn.setText(product.getString("turn"));
}else{ }
}
}
答案 0 :(得分:3)
您可以让doInBackground
返回JSONObject json
并在onPostExecute
中使用它。
然后检查一切是否正常,请在onPostExecute
内进行检查。
类似的东西:
class GetProductDetails extends AsyncTask<String, String, JSONObject>
{
@Override
protected void onPreExecute() {
//bla-bla-bla
}
protected JSONObject doInBackground(String... args) {
JSONObject json = null;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("pid", pid));
json = jsonParser.makeHttpRequest(
url_product_detials, "GET", params);
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
if (json == null) return; // check if json is null too! if null something went wrong (handle it, i used return as example)
int success = json.getInt(TAG_SUCCESS);;
if (success == 1) {
JSONArray productObj = json.getJSONArray(TAG_PRODUCT);
JSONObject product = productObj.getJSONObject(0);
txtName = (TextView) findViewById(R.id.tvName);
txtPrice = (TextView) findViewById(R.id.tvPrice);
txtDesc = (TextView) findViewById(R.id.tvDescription);
txtCurrent = (TextView) findViewById(R.id.tvCurrent);
txtTurn = (TextView) findViewById(R.id.tvTurn);
txtName.setText(product.getString(TAG_NAME));
txtPrice.setText(product.getString(TAG_PRICE));
txtDesc.setText(product.getString(TAG_DESCRIPTION));
txtCurrent.setText(product.getString("current"));
txtTurn.setText(product.getString("turn"));
}else{
}
}
答案 1 :(得分:0)
在这里你不应该使用AsyncTask,试试这个库---- android-async-http,它由Instagram,Pinterest和其他着名的Android应用程序使用。有了这个库,你可以像这样完成你的工作:
RequestParams params = new RequestParams();
params.put("username", "james");
params.put("password", "123456");
params.put("email", "my@email.com");
params.put("profile_picture", new File("pic.jpg")); // Upload a File
params.put("profile_picture2", someInputStream); // Upload an InputStream
params.put("profile_picture3", new ByteArrayInputStream(someBytes)); // Upload some bytes
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://youdomain.com", params, new AsyncHttpResponseHandler(){
@Override
public void onSuccess(String result) {
// Do something here or change your ui according the result.
}
});
答案 2 :(得分:0)
Java提供的接口非常强大,可以为您的类添加行为,在您希望AsyncTask子类为1-retreive数据和db存储2-store的情况下,可以在后台线程上完成,没有任何问题。和你的活动来更新它的UI,所以你有三个行为,你的问题是第三种行为,你可以通过界面轻松克服这个问题。
UIUpdate.java
public interface UIUpdate
{
public void updateFields(JSONObject json);
}
MainActivity.java
public class MainActivity extends Activity implements UIUpdate
{
public void execute()
{
new GetProductDetails(this).execute("args");
}
....
@override
public void updateFields(JSONObject json)
{
JSONArray productObj = json.getJSONArray(TAG_PRODUCT);
JSONObject product = productObj.getJSONObject(0);
txtName = (TextView) findViewById(R.id.tvName);
txtPrice = (TextView) findViewById(R.id.tvPrice);
txtDesc = (TextView) findViewById(R.id.tvDescription);
txtCurrent = (TextView) findViewById(R.id.tvCurrent);
txtTurn = (TextView) findViewById(R.id.tvTurn);
txtName.setText(product.getString(TAG_NAME));
txtPrice.setText(product.getString(TAG_PRICE));
txtDesc.setText(product.getString(TAG_DESCRIPTION));
txtCurrent.setText(product.getString("current"));
txtTurn.setText(product.getString("turn"));
}
}
GetProductDetails.java
class GetProductDetails extends AsyncTask<String, String, JSONObject>
{
UIUpdate listener;
public GetProductDetails( UIUpdate listener )
{
this.listener=listener;
}
protected JSONObject doInBackground(String... args)
{
JSONObject json = null;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("pid", pid));
json = jsonParser.makeHttpRequest(
url_product_detials, "GET", params);
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
protected void onPostExecute(JSONObject json)
{
pDialog.dismiss();
if (json == null) return; // check if json is null too! if null something went wrong (handle it, i used return as example)
int success = json.getInt(TAG_SUCCESS);
if (success == 1)
{
listener.updateFields(json);
}
else{ }
}
}
此外,您需要从doInBackGround()中重新启动JSONObject以在postExecute()中使用。