2013-03-15 51 views
0

我有一个单独的类为异步任务。我如何将异步任务类的值返回给主类。谁能帮我?如何从android中的异步任务返回值?

在主类调用异步任务类

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(); 
    } 
} 
+0

你的主类调用一些方法? – Sajmon 2013-03-15 11:52:06

+0

http://stackoverflow.com/a/9963705/1434631 – Nermeen 2013-03-15 11:52:45

+0

@Sajmon ya将字符串值异步任务类返回到主类。 – Yugesh 2013-03-15 12:16:22

回答

0

通常,我在main class中创建我的AsyncTasks。但是,如果您使用不同的文件,则可以在asynctask中创建一个getter方法。你可以添加一个OnPostExecuteListener,当它被调用时,你可以调用成员方法来获得结果。

+0

@Hardik如何使用getter方法。 – Yugesh 2013-03-15 12:15:11

+0

public yourObyectType getMyResult(){ return myResult; } – 2013-03-15 12:23:42

+0

我没有得到一个值。 – Yugesh 2013-03-15 13:09:14

0

您可以让你传递给异步类或该变量的简单get方法的接口。

+0

如何使用get方法。 – Yugesh 2013-03-15 12:18:21

+0

@Yugesh get方法只是返回你需要的值,但你应该去看一些'AsyncTask'的例子。你可能需要的仅仅是使用'onPostExecute'方法来处理返回的结果。 – 2013-03-15 12:26:22

0

回调方法怎么样?

您将主类作为参数传递。比你想要使用返回的字符串onPostExecute()

+0

如何回调主类,在postexecute()过程中我认为在后台完成。 – Yugesh 2013-03-17 06:46:31