非同期タスク用に別のクラスがあります。非同期タスク クラスからメイン クラスに値を返すにはどうすればよいですか。誰でも私を助けることができますか?
メインクラスで非同期タスククラスを呼び出す
String product_id,av_quantity;
Stock_updatetask = new Stock_update();
Stock_updatetask.execute(product_id,av_quantity);
非同期タスク クラス
public class Stock_update extends AsyncTask<String, Void, String> {
JSONObject json = new JSONObject();
JSONArray jsonarray;
String stock_update;
protected String doInBackground(String... params) {
try {
// checkInternetConnection();
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),20000);
HttpConnectionParams.setSoTimeout(client.getParams(), 20000);
HttpResponse response;
HttpPost post = new HttpPost("http://www.name.in/cakefoodnew/customer/stockUpdate?json=");
json.put("submenu_id", "" + params[0]);
json.put("available_quantity", "" + params[1]);
// Log.v("id", ""+json);
post.setHeader("json", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
post.setEntity(se);
response = client.execute(post);
if (response != null) {
// get a data
InputStream in = response.getEntity().getContent();
String a = convertStreamToString(in);
// Log.v("id", ""+a);
try {
jsonarray = new JSONArray("[" + a + "]");
json = jsonarray.getJSONObject(0);
stock_update = (json.getString("Success"));
stock_update文字列値をメイン クラスに返す方法。
} catch (Exception e) {
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
/*
* if (stock_update.equalsIgnoreCase("1")) {
*
* progressDialog.dismiss();
* Toast.makeText(getApplicationContext(),"Stock updated",
* 700).show();
*
* }
*
* else if (stock_update.equalsIgnoreCase("0")){
*
* progressDialog.dismiss();
* Toast.makeText(getApplicationContext(),"Stock not updated",
* 700).show();
*
* }
*/
}
// Json response
private String convertStreamToString(InputStream is) {
// TODO Auto-generated method stub
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}