我调用我的web服务并使用回调制作动态代码。最初我发送响应字符串但是我正在使用gjson库,这对我不起作用。现在我发送但是得到这个例外..
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)
RequestTask类
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;
}
}
and **call back** like that
public interface WebserviceCallBack {
public void getWebserviceResponse(InputStream result);
}
and **main activity** like that
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("-----", "pallavi");
}
持有人类;
public class Holder {
List<deparaturedaseboarddto> data;
}
我可以发送响应字符串而不是使用gson库吗?