2015-04-26 72 views
2

運行時崩潰我的應用程序時出現以下代碼。我打電話給MainActivity。當運行它告訴我:片段中的AsyncTask無法正常工作並顯示致命異常:AsyncTask#1

FATAL EXCEPTION: AsyncTask #1,並指示我向ProgressDialogHttp Response

import android.app.Fragment; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.ListView; 

import com.montel.senarivesselapp.model.ShowDataList; 
import com.montel.senarivesselapp.model.Vessel; 

import org.apache.http.HttpResponse; 
import org.apache.http.HttpStatus; 
import org.apache.http.StatusLine; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpResponseException; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.util.ArrayList; 

public class ShowallFragment extends Fragment { 

    //variables for JSON query 
    private static final String name ="vessel_name"; 
    private static final String etad="eta_date"; 
    private static final String etat="eta_time"; 
    private static final String etbd="etb_date"; 
    private static final String etbt="etb_time"; 
    private static final String shippingName="shipping_agent_name"; 
    private static final String v1 = "vessels"; 
    private static final String v2 = "Vessel"; 


    private ArrayList<Vessel> vList = new ArrayList<>(); 
    private ArrayAdapter arrayAdapter = null; 
    private ListView listView = null; 
    private EditText et = null; 
    private ShowDataList ssadapter = null; 
    private View rootView; 

    public ShowallFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     rootView = inflater.inflate(R.layout.fragment_showall, container, false); 
     setContentView(R.layout.fragment_showall); 
     setRetainInstance(true); 


     return rootView; 
    } 

    private void setContentView(int fragment_showall) { 

    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
    } 


    //For getting the JSON schedule from server 
    class Schedule extends AsyncTask<String, String, String> { 
     ProgressDialog loadDialog; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      //Show the loading dialog 
      loadDialog = new ProgressDialog(ShowallFragment.this); 
      loadDialog.setMessage("Please wait...."); 
      loadDialog.setCancelable(false); 
      loadDialog.show(); 

     } 


     @Override 
     protected String doInBackground(String... uri) { 
      BufferedReader input = null; 
      String data = null; 

      try { 

       DefaultHttpClient client = new DefaultHttpClient(); 

       HttpResponse response = client.execute(new HttpGet("http://")); 
       StatusLine stline = response.getStatusLine(); 
       if (stline.getStatusCode() == HttpStatus.SC_OK) { 
        ByteArrayOutputStream out = new ByteArrayOutputStream(); 
        response.getEntity().writeTo(out); 
        data = out.toString(); 
        out.close(); 
       } else { 
        response.getEntity().getContent().close(); 
        throw new IOException(stline.getReasonPhrase()); 
       } 
      } catch (HttpResponseException he) { 
       he.printStackTrace(); 
      } catch (ClientProtocolException cpe) { 
       cpe.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       if (input != null) { 
        try { 
         input.close(); 
         return data; 
        } catch (Exception e) { 
         System.out.println(e); 
        } 
       } 
      } 
      return data; 
     } 
    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     //Fill the vList array 
     Log.d("POST EXECUTE", result.toString()); 
     loadDialog.dismiss(); 
     if (result != null) { 
      try { 
       JSONObject jo = new JSONObject(result); 
       JSONArray vessels = jo.getJSONArray(v1); 

       Log.d("VESSEL LOG", vessels.toString()); 
       vList = new ArrayList(); 

       for (int i = 0; i < vessels.length(); i++) { 
        JSONObject vv = vessels.getJSONObject(i); 
        Log.d("VESSEL NAME", vv.getJSONObject(v2).getString(name)); 

        vList.add(new Vessel(vv.getJSONObject(v2).getString(name), 
          vv.getJSONObject(v2).getString(etad), 
          vv.getJSONObject(v2).getString(etat), 
          vv.getJSONObject(v2).getString(etbd), 
          vv.getJSONObject(v2).getString(etbt), 
            vv.getJSONObject(v2).getString(shippingName))); 

       } 


      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } 

     listView = (ListView) rootView.findViewById(R.id.dataShow); 
     listView.setAdapter(arrayAdapter); 
     ssadapter = new ShowDataList(ShowallFragment.this , vList); 
     listView.setAdapter(ssadapter); 
    } 




    } 
} 
+0

從logcat發佈的例外 – nikis

+0

請顯示您如何從主要活動調用任務。 – greenapps

+0

你也可以使用Volley ..它比Async Task快。 – Android

回答

0
SecurityException: Permission denied (missing INTERNET permission 

因此,您應該在您的項目中申請INTERNET權限AndroidManifest.xml文件。在相同的位置添加:

​​
0

onPostExecute()改變Log.d("POST EXECUTE", result.toString());位置在裏面抓因此您的代碼看起來就像這樣:

catch (Exception ee) { 
        ee.printStackTrace(); 
       } 
catch (JSONException e) { 
       Log.d("POST EXECUTE", e.toString()); 
      } // catch (JSONException e) 
+0

不對。不在catch塊中,而在try塊中。但是,我已經告訴你,你可以閱讀。 – greenapps

+0

對不起,我沒有注意到你指出了,但正如你可以看到我沒有使用result.toString(),而是我使用e.toString(),它適用於我。 –