2017-07-24 99 views
-2

我試圖從web服務獲取json響應並將其放入arraylist中。然後,我想讓arraylist進入recyclerview。我使用asynctask在後臺執行它。 Web服務的響應是一個json數組。AsyncTask doInBackground返回arraylist

public class MainActivity extends AppCompatActivity { 

    String nombreChef; 
    Double ratingChef; 

    private RecyclerView rvMain; 
    private RVMainAdapter mRVMainAdapter; 

    private ArrayList<Chef> data= new ArrayList<>(); 

    private class AsyncCallWS extends AsyncTask<Void, Void, Void>{ 

     @Override 
     protected void onPreExecute() { 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 

      SoapObject soapObject = new SoapObject(Conexion.NAMESPACE, Conexion.METHOD_NAME_CHEFS_CERCA); 
      soapObject.addProperty("idUser", Integer.valueOf(5)); 
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      envelope.dotNet = true; 
      envelope.setOutputSoapObject(soapObject); 
      HttpTransportSE httpTransportSE = new HttpTransportSE(Conexion.URL); 

      try { 
       httpTransportSE.call(Conexion.SOAP_ACTION_TPNAME, envelope); 
       String resultado = envelope.getResponse().toString(); 
       JSONObject json = new JSONObject(resultado); 
       nombreChef=json.getString("nombre_chef"); 
       ratingChef=json.getDouble("rating"); 

       //IM NOT SURE HOW TO PUT THE ARRAY RESPONSE INTO ARRAYLIST???? 
       data.add(new Chef(12,nombreChef,"Riuos","21","Jr los belepos",12,"Delivery",ratingChef.toString(),"Activo")); 
      } 
      catch (Exception e) { 
       Log.e("Estacion", e.getMessage()); 
       //result = "0"; 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 

     } 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     AsyncCallWS task = new AsyncCallWS(); 
     task.execute(); 

     rvMain = (RecyclerView) findViewById(R.id.rv_prueba); 
     rvMain.setLayoutManager(new LinearLayoutManager(MainActivity.this)); 

     mRVMainAdapter = new RVMainAdapter(data); 
     rvMain.setAdapter(mRVMainAdapter); 

    } 

這是來自Web服務的響應GSON:

enter image description here

+0

谷歌「asynctasks如何工作」 –

+0

我沒有得到你想告訴我的東西 – Pierre

+0

請閱讀[在什麼情況下,我可以添加「緊急」或其他類似的短語,以獲得更快的答案? (//meta.stackoverflow.com/q/326569) - 總結是,這不是解決志願者的理想方式,並且可能會對獲得答案產生反作用。請不要將這添加到您的問題。 – halfer

回答

0

則需要由使用一些庫或通過自己的Android使用JSON類有解析JOSN響應。 您可以通過谷歌爲此獲取示例。 通過做自己爲例:

//Convert response string to JOSN array 
JSONArray json = new JSONArray(responseText); 

,那麼你需要遍歷這個數組以得到/從中提取相應的數據。返回此解析數據以獲取onPostExecute中的此解析數據,您可以從onPostExecute中更新您的列表適配器或對UI進行任何更新。

你可以參考this

+0

我應該保存在responseText中? – Pierre

+0

String responseText = envelope.getResponse()。toString(); 然後解析數據並將數據添加到arraylist,就像你剛纔提到的那樣,並從doInBackground返回這個數組列表。 – AndroidDevUser

0
private class AsyncCallWS extends AsyncTask<Void, Void, ArrayList<Chef>>{ 

    @Override 
    protected void onPreExecute() { 
    } 

    @Override 
    protected ArrayList<Chef> doInBackground(Void... params) { 
     ArrayList<Chef> data= new ArrayList<>(); 
     SoapObject soapObject = new SoapObject(Conexion.NAMESPACE, Conexion.METHOD_NAME_CHEFS_CERCA); 
     soapObject.addProperty("idUser", Integer.valueOf(5)); 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(soapObject); 
     HttpTransportSE httpTransportSE = new HttpTransportSE(Conexion.URL); 

     try { 
      httpTransportSE.call(Conexion.SOAP_ACTION_TPNAME, envelope); 
      String resultado = envelope.getResponse().toString(); 
      JSONObject json = new JSONObject(resultado); 
      nombreChef=json.getString("nombre_chef"); 
      ratingChef=json.getDouble("rating"); 

      //IM NOT SURE HOW TO PUT THE ARRAY RESPONSE INTO ARRAYLIST???? 
      data.add(new Chef(12,nombreChef,"Riuos","21","Jr los belepos",12,"Delivery",ratingChef.toString(),"Activo")); 
     } 
     catch (Exception e) { 
      Log.e("Estacion", e.getMessage()); 
      //result = "0"; 
     } 

     return data; 
    } 

    @Override 
    protected void onPostExecute(ArrayList<Chef> result) { 
     super.onPostExecute(result) 

    // your result.(ArrayList<Chef>) 
    } 

再次檢查你的JSON解析。

相關問題