Android 片段中的AsyncTask不工作并显示致命异常:AsyncTask#1
以下代码在运行时使我的应用程序崩溃。我在Android 片段中的AsyncTask不工作并显示致命异常:AsyncTask#1,android,android-asynctask,httpresponse,progressdialog,Android,Android Asynctask,Httpresponse,Progressdialog,以下代码在运行时使我的应用程序崩溃。我在main活动中调用它。运行时,它告诉我: 致命异常:AsyncTask#1并将我引向ProgressDialog和Http响应行 import android.app.Fragment; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.L
main活动中调用它。运行时,它告诉我:
致命异常:AsyncTask#1
并将我引向ProgressDialog
和Http响应
行
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);
}
}
}
导入android.app.Fragment;
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.EditText;
导入android.widget.ListView;
导入com.montel.senarivesselapp.model.ShowDataList;
进口com.montel.senarivesselapp.model.Vessel;
导入org.apache.http.HttpResponse;
导入org.apache.http.HttpStatus;
导入org.apache.http.StatusLine;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpResponseException;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.BufferedReader;
导入java.io.ByteArrayOutputStream;
导入java.io.IOException;
导入java.util.ArrayList;
公共类ShowallFragment扩展了片段{
//JSON查询的变量
私有静态最终字符串name=“船只名称”;
私有静态最终字符串etad=“eta_date”;
私有静态最终字符串etat=“eta_time”;
私有静态最终字符串etbd=“etb_date”;
私有静态最终字符串etbt=“etb_time”;
私有静态最终字符串shippingName=“shipping\u agent\u name”;
专用静态最终字符串v1=“船舶”;
专用静态最终字符串v2=“容器”;
private ArrayList vList=新的ArrayList();
私有ArrayAdapter ArrayAdapter=null;
私有ListView ListView=null;
私有EditText et=null;
私有ShowDataList ssadapter=null;
私有视图rootView;
公共ShowallFragment(){
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
rootView=充气机。充气(R.layout.fragment\u showall,容器,false);
setContentView(R.layout.fragment\u showall);
setRetainInstance(真);
返回rootView;
}
私有void setContentView(int fragment_showall){
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
}
//用于从服务器获取JSON计划
类调度扩展了异步任务{
进程对话框加载对话框;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示加载对话框
loadDialog=newProgressDialog(ShowallFragment.this);
loadDialog.setMessage(“请稍候…”);
loadDialog.setCancelable(false);
loadDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…uri){
BufferedReader输入=null;
字符串数据=null;
试一试{
DefaultHttpClient=新的DefaultHttpClient();
HttpResponse response=client.execute(新的HttpGet(“http://”);
StatusLine stline=response.getStatusLine();
if(stline.getStatusCode()==HttpStatus.SC\u确定){
ByteArrayOutputStream out=新建ByteArrayOutputStream();
response.getEntity().writeTo(out);
data=out.toString();
out.close();
}否则{
response.getEntity().getContent().close();
抛出新IOException(stline.getReasonPhrase());
}
}捕获(HttpResponseException he){
he.printStackTrace();
}捕获(客户端协议异常cpe){
printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
如果(输入!=null){
试一试{
input.close();
返回数据;
}捕获(例外e){
系统输出打印ln(e);
}
}
}
返回数据;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
//填充vList数组
Log.d(“POST-EXECUTE”,result.toString());
loadDialog.disclose();
如果(结果!=null){
试一试{
JSONObject jo=新的JSONObject(结果);
JSONArray船只=jo.getJSONArray(v1);
Log.d(“容器日志”,Vessers.toString());
vList=新的ArrayList();
对于(int i=0;i
在onPostExecute()
中更改Log.d的位置(
catch (Exception ee) {
ee.printStackTrace();
}
catch (JSONException e) {
Log.d("POST EXECUTE", e.toString());
} // catch (JSONException e)
SecurityException: Permission denied (missing INTERNET permission
<uses-permission android:name="android.permission.INTERNET" />