10-23 00:41:00.705: E/AndroidRuntime(3622): FATAL EXCEPTION: AsyncTask #1 10-23 00:41:00.705: E/AndroidRuntime(3622): java.lang.RuntimeException: An error occured while executing doInBackground() 10-23 00:41:00.705: E/AndroidRuntime(3622): at android.os.AsyncTask$3.done(AsyncTask.java:299) 10-23 00:41:00.705: E/AndroidRuntime(3622): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 10-23 00:41:00.705: E/AndroidRuntime(3622): at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 10-23 00:41:00.705: E/AndroidRuntime(3622): at java.util.concurrent.FutureTask.run(FutureTask.java:239) 10-23 00:41:00.705: E/AndroidRuntime(3622): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 10-23 00:41:00.705: E/AndroidRuntime(3622): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 10-23 00:41:00.705: E/AndroidRuntime(3622): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 10-23 00:41:00.705: E/AndroidRuntime(3622): at java.lang.Thread.run(Thread.java:856) 10-23 00:41:00.705: E/AndroidRuntime(3622): Caused by: java.lang.IllegalStateException: Content has been consumed 10-23 00:41:00.705: E/AndroidRuntime(3622): at org.apache.http.entity.BasicHttpEntity.getContent(BasicHttpEntity.java:84) 10-23 00:41:00.705: E/AndroidRuntime(3622): at org.apache.http.entity.BasicHttpEntity.writeTo(BasicHttpEntity.java:126) 10-23 00:41:00.705: E/AndroidRuntime(3622): at org.apache.http.entity.HttpEntityWrapper.writeTo(HttpEntityWrapper.java:101) 10-23 00:41:00.705: E/AndroidRuntime(3622): at org.apache.http.conn.BasicManagedEntity.writeTo(BasicManagedEntity.java:126) 10-23 00:41:00.705: E/AndroidRuntime(3622): at com.firstgroup.webservice.RequestTask.doInBackground(RequestTask.java:51) 10-23 00:41:00.705: E/AndroidRuntime(3622): at com.firstgroup.webservice.RequestTask.doInBackground(RequestTask.java:1) 10-23 00:41:00.705: E/AndroidRuntime(3622): at android.os.AsyncTask$2.call(AsyncTask.java:287) 10-23 00:41:00.705: E/AndroidRuntime(3622): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 10-23 00:41:00.705: E/AndroidRuntime(3622): ... 4 more
你好
我正在使用gson库调用我的Web服务,但出现异常。我不知道为什么会发生。您能告诉我删除此错误吗,我将向您提供我的工作步骤.. 1)Main activity
Main activity
public class Appliacationload extends Activity implements WebserviceCallBack{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.appliacationload); /*WebserviceMethod callDepartudeDashboard=new WebserviceMethod(); callDepartudeDashboard.setObserver(this); callDepartudeDashboard.getwebService(ConstantVariable.dashboardWebServiceURL+"a/"+"arrival"+"?crsCode=hnh");*/ RequestTask callWebService=new RequestTask(); callWebService.setObserver(this); callWebService.execute(ConstantVariable.dashboardWebServiceURL+"a/"+"arrival"+"?crsCode=vic"); } @Override public void getWebserviceResponse(String response) { // TODO Auto-generated method stub Log.d("response", response); } }
我有一个holder和RequestTask类来调用webservice并返回回叫。
public class RequestTask extends AsyncTask<String, String, String>{ private WebserviceCallBack callBack; private ProgressDialog progressDialog; @Override protected void onPreExecute() { // TODO Auto-generated method stub progressDialog= new ProgressDialog((Context) callBack); super.onPreExecute(); progressDialog.setTitle("Please Wait..."); progressDialog.setMessage("Webservice Call..."); progressDialog.setCancelable(true); progressDialog.show(); } @Override protected String doInBackground(String... uri) { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response; String responseString = null; try { response = httpclient.execute(new HttpGet(uri[0])); StatusLine statusLine = response.getStatusLine(); if(statusLine.getStatusCode() == HttpStatus.SC_OK){ Reader reader = new InputStreamReader(response.getEntity().getContent()); Gson gson = new Gson(); Holder response1 = gson.fromJson(reader, Holder.class); ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); responseString = out.toString(); } else{ //Closes the connection. response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); } } catch (ClientProtocolException e) { //TODO Handle problems..\ progressDialog.hide(); } catch (IOException e) { //TODO Handle problems.. progressDialog.hide(); } return responseString; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); //Do anything with response.. if(callBack!=null){ callBack.getWebserviceResponse(result); } progressDialog.hide(); } public void setObserver(WebserviceCallBack callback){ callBack=callback; } }
持有人班;
public class Holder { List<deparaturedaseboarddto> data; }
*当我像这样更改请求任务时的 *新更新 ..
public class RequestTask extends AsyncTask<String, String, InputStream>{ private WebserviceCallBack callBack; private ProgressDialog progressDialog; @Override protected void onPreExecute() { // TODO Auto-generated method stub progressDialog= new ProgressDialog((Context) callBack); super.onPreExecute(); progressDialog.setTitle("Please Wait..."); progressDialog.setMessage("Webservice Call..."); progressDialog.setCancelable(true); progressDialog.show(); } @Override protected InputStream doInBackground(String... uri) { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response; String responseString = null; try { response = httpclient.execute(new HttpGet(uri[0])); StatusLine statusLine = response.getStatusLine(); if(statusLine.getStatusCode() == HttpStatus.SC_OK){ /*Reader reader = new InputStreamReader(response.getEntity().getContent()); Gson gson = new Gson(); Holder response1 = gson.fromJson(reader, Holder.class); ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); responseString = out.toString();*/ return response.getEntity().getContent(); } else{ //Closes the connection. response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); } } catch (ClientProtocolException e) { //TODO Handle problems..\ progressDialog.hide(); } catch (IOException e) { //TODO Handle problems.. progressDialog.hide(); } return null; } @Override protected void onPostExecute(InputStream result) { super.onPostExecute(result); //Do anything with response.. if(callBack!=null){ callBack.getWebserviceResponse(result); } progressDialog.dismiss(); progressDialog=null; } public void setObserver(WebserviceCallBack callback){ callBack=callback; } }
然后像这样 回电
public interface WebserviceCallBack { public void getWebserviceResponse(InputStream result); }
和 主要活动 一样,
public class Appliacationload extends Activity implements WebserviceCallBack{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.appliacationload); /*WebserviceMethod callDepartudeDashboard=new WebserviceMethod(); callDepartudeDashboard.setObserver(this); callDepartudeDashboard.getwebService(ConstantVariable.dashboardWebServiceURL+"a/"+"arrival"+"?crsCode=hnh");*/ RequestTask callWebService=new RequestTask(); callWebService.setObserver(this); callWebService.execute(ConstantVariable.dashboardWebServiceURL+"a/"+"arrival"+"?crsCode=vic"); } @Override public void getWebserviceResponse(InputStream result) { // TODO Auto-generated method stub Reader reader = new InputStreamReader(result); Gson gson = new Gson(); Holder response1 = gson.fromJson(reader, Holder.class); Log.d("-----", "naveen"); }
我得到这个例外,为什么..?我在回叫时发送输入流,并在主要活动中进行。
例外:
10-23 03:07:33.956: E/AndroidRuntime(22412): FATAL EXCEPTION: main 10-23 03:07:33.956: E/AndroidRuntime(22412): android.os.NetworkOnMainThreadException 10-23 03:07:33.956: E/AndroidRuntime(22412): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 10-23 03:07:33.956: E/AndroidRuntime(22412): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163) 10-23 03:07:33.956: E/AndroidRuntime(22412): at libcore.io.IoBridge.recvfrom(IoBridge.java:513) 10-23 03:07:33.956: E/AndroidRuntime(22412): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488) 10-23 03:07:33.956: E/AndroidRuntime(22412): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46) 10-23 03:07:33.956: E/AndroidRuntime(22412): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240) 10-23 03:07:33.956: E/AndroidRuntime(22412): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103) 10-23 03:07:33.956: E/AndroidRuntime(22412): at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134) 10-23 03:07:33.956: E/AndroidRuntime(22412): at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:161) 10-23 03:07:33.956: E/AndroidRuntime(22412): at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:159) 10-23 03:07:33.956: E/AndroidRuntime(22412): at java.io.InputStreamReader.read(InputStreamReader.java:244) 10-23 03:07:33.956: E/AndroidRuntime(22412): at com.google.gson.stream.JsonReader.fillBuffer(JsonReader.java:1298) 10-23 03:07:33.956: E/AndroidRuntime(22412): at com.google.gson.stream.JsonReader.nextQuotedValue(JsonReader.java:1028) 10-23 03:07:33.956: E/AndroidRuntime(22412): at com.google.gson.stream.JsonReader.nextName(JsonReader.java:796) 10-23 03:07:33.956: E/AndroidRuntime(22412): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:178) 10-23 03:07:33.956: E/AndroidRuntime(22412): at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40) 10-23 03:07:33.956: E/AndroidRuntime(22412): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81) 10-23 03:07:33.956: E/AndroidRuntime(22412): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60) 10-23 03:07:33.956: E/AndroidRuntime(22412): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:95) 10-23 03:07:33.956: E/AndroidRuntime(22412): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:183) 10-23 03:07:33.956: E/AndroidRuntime(22412): at com.google.gson.Gson.fromJson(Gson.java:805) 10-23 03:07:33.956: E/AndroidRuntime(22412): at com.google.gson.Gson.fromJson(Gson.java:743) 10-23 03:07:33.956: E/AndroidRuntime(22412): at com.firstgroup.applicationload.Appliacationload.getWebserviceResponse(Appliacationload.java:48) 10-23 03:07:33.956: E/AndroidRuntime(22412): at com.firstgroup.webservice.RequestTask.onPostExecute(RequestTask.java:77) 10-23 03:07:33.956: E/AndroidRuntime(22412): at com.firstgroup.webservice.RequestTask.onPostExecute(RequestTask.java:1) 10-23 03:07:33.956: E/AndroidRuntime(22412): at android.os.AsyncTask.finish(AsyncTask.java:631) 10-23 03:07:33.956: E/AndroidRuntime(22412): at android.os.AsyncTask.access$600(AsyncTask.java:177) 10-23 03:07:33.956: E/AndroidRuntime(22412): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) 10-23 03:07:33.956: E/AndroidRuntime(22412): at android.os.Handler.dispatchMessage(Handler.java:99) 10-23 03:07:33.956: E/AndroidRuntime(22412): at android.os.Looper.loop(Looper.java:137) 10-23 03:07:33.956: E/AndroidRuntime(22412): at android.app.ActivityThread.main(ActivityThread.java:5041) 10-23 03:07:33.956: E/AndroidRuntime(22412): at java.lang.reflect.Method.invokeNative(Native Method) 10-23 03:07:33.956: E/AndroidRuntime(22412): at java.lang.reflect.Method.invoke(Method.java:511) 10-23 03:07:33.956: E/AndroidRuntime(22412): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 10-23 03:07:33.956: E/AndroidRuntime(22412): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 10-23 03:07:33.956: E/AndroidRuntime(22412): at dalvik.system.NativeStart.main(Native Method)
原因是,你只能消耗Content的Entity一次。
Content
Entity
您在这里做了两次(可能不知道)
Reader reader = new InputStreamReader(response.getEntity().getContent());
和这里
response.getEntity().writeTo(out);
我知道这听起来有些怪异,但实际上writeTo()函数将从实体获取内容并将其写入OutputStream。您可以在此处的文档中看到它
OutputStream
您可以使用的另一种解决方法是将其转换为字符串,然后让GSON处理
@Override protected String doInBackground(String... uri) { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response; String responseString = null; try { response = httpclient.execute(new HttpGet(uri[0])); StatusLine statusLine = response.getStatusLine(); if(statusLine.getStatusCode() == HttpStatus.SC_OK){ responseString = EntityUtils.toString(response.getEntity()); // content will be consume only once Gson gson = new Gson(); Holder response1 = gson.fromJson(responseString, Holder.class); } else{ //Closes the connection. response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); } } catch (ClientProtocolException e) { //TODO Handle problems..\ progressDialog.hide(); } catch (IOException e) { //TODO Handle problems.. progressDialog.hide(); } return responseString; }
希望我的回答能对您有所帮助!