我一直试图从我的dotnet webservice获取一些数据,并将结果以json格式传递给我的android应用程序。通过调用doInBackground
,在invokeJSONWS
函数(被覆盖)中检索json数据。当我尝试调用publishProgress
方法时,这会给我一个错误。我需要将json数据传递给这个publishProgress
方法。我需要覆盖publishProgress
,因为它的返回类型是string []。
private class ChartTask extends AsyncTask< String,Void, Void>{
// Generates dummy data in a non-ui thread
@Override
protected Void doInBackground(String... params) {
int i = 0;
invokeJSONWS(params[0],"PopulateCities");
return null;
try{
do{
String [] values = new String[2];
Random r = new Random();
int visits = r.nextInt(27);
values[0] = Integer.toString(i);
values[1] = Integer.toString(visits);
publishProgress(values);
//Error here:The method publishProgress(Void...) in the type AsyncTask<String,Void,Void> is not applicable for the arguments (String[])
Thread.sleep(1000);
i++;
}while(i<=30);
}catch(Exception e){ }
return null;
}
// Plotting generated data in the graph
@Override
protected void onProgressUpdate(String[] values) {
visitsSeries.add(Integer.parseInt(values[0]), Integer.parseInt(values[1]));
mChart.repaint();
}
//error here:The method onProgressUpdate(String[]) of type MainActivity.ChartTask must override or implement a supertype method
}
//Method which invoke web methods
public void invokeJSONWS(String country, String methName) {
// Create request
SoapObject request = new SoapObject(NAMESPACE, methName);
// Property which holds input parameters
PropertyInfo paramPI = new PropertyInfo();
// Set Name
paramPI.setName("country");
// Set Value
paramPI.setValue(country);
// Set dataType
paramPI.setType(String.class);
// Add the property to request object
request.addProperty(paramPI);
// Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
// Set output SOAP object
envelope.setOutputSoapObject(request);
// Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
// Invole web service
androidHttpTransport.call(SOAP_ACTION+methName, envelope);
// Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
// Assign it to static variable
responseJSON = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
有没有解决这个问题的方法? 在此先感谢!!
Logcat看起来像这样
04-18 05:55:24.680: W/dalvikvm(1324): VFY: unable to resolve new-instance 430 (Lcom/google/gson/Gson;) in Lin/wptrafficanalyzer/achartenginedynamicchart/MainActivity;
04-18 05:55:24.680: D/dalvikvm(1324): VFY: replacing opcode 0x22 at 0x000f
04-18 05:55:24.820: E/dalvikvm(1324): Could not find class 'org.ksoap2.serialization.SoapObject', referenced from method in.wptrafficanalyzer.achartenginedynamicchart.MainActivity.invokeJSONWS
04-18 05:55:24.820: W/dalvikvm(1324): VFY: unable to resolve new-instance 584 (Lorg/ksoap2/serialization/SoapObject;) in Lin/wptrafficanalyzer/achartenginedynamicchart/MainActivity;
04-18 05:55:24.850: D/dalvikvm(1324): VFY: replacing opcode 0x22 at 0x0000
04-18 05:55:24.850: D/dalvikvm(1324): DexOpt: unable to opt direct call 0x0c0f at 0x11 in Lin/wptrafficanalyzer/achartenginedynamicchart/MainActivity;.<init>
04-18 05:55:24.860: D/dalvikvm(1324): DexOpt: unable to opt direct call 0x0fd1 at 0x04 in Lin/wptrafficanalyzer/achartenginedynamicchart/MainActivity;.invokeJSONWS
04-18 05:55:24.860: D/dalvikvm(1324): DexOpt: unable to opt direct call 0x0fcd at 0x09 in Lin/wptrafficanalyzer/achartenginedynamicchart/MainActivity;.invokeJSONWS
04-18 05:55:24.860: D/dalvikvm(1324): DexOpt: unable to opt direct call 0x0fd4 at 0x20 in Lin/wptrafficanalyzer/achartenginedynamicchart/MainActivity;.invokeJSONWS
04-18 05:55:24.860: D/dalvikvm(1324): DexOpt: unable to opt direct call 0x0fd7 at 0x2d in Lin/wptrafficanalyzer/achartenginedynamicchart/MainActivity;.invokeJSONWS
04-18 05:55:24.890: D/AndroidRuntime(1324): Shutting down VM
04-18 05:55:24.900: W/dalvikvm(1324): threadid=1: thread exiting with uncaught exception (group=0xb2ac5ba8)
04-18 05:55:24.920: E/AndroidRuntime(1324): FATAL EXCEPTION: main
04-18 05:55:24.920: E/AndroidRuntime(1324): Process: in.wptrafficanalyzer.achartenginedynamicchart, PID: 1324
04-18 05:55:24.920: E/AndroidRuntime(1324): java.lang.NoClassDefFoundError: com.google.gson.Gson
04-18 05:55:24.920: E/AndroidRuntime(1324): at in.wptrafficanalyzer.achartenginedynamicchart.MainActivity.<init>(MainActivity.java:43)
04-18 05:55:24.920: E/AndroidRuntime(1324): at java.lang.Class.newInstanceImpl(Native Method)
04-18 05:55:24.920: E/AndroidRuntime(1324): at java.lang.Class.newInstance(Class.java:1208)
04-18 05:55:24.920: E/AndroidRuntime(1324): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
04-18 05:55:24.920: E/AndroidRuntime(1324): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
04-18 05:55:24.920: E/AndroidRuntime(1324): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-18 05:55:24.920: E/AndroidRuntime(1324): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-18 05:55:24.920: E/AndroidRuntime(1324): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-18 05:55:24.920: E/AndroidRuntime(1324): at android.os.Handler.dispatchMessage(Handler.java:102)
04-18 05:55:24.920: E/AndroidRuntime(1324): at android.os.Looper.loop(Looper.java:136)
04-18 05:55:24.920: E/AndroidRuntime(1324): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-18 05:55:24.920: E/AndroidRuntime(1324): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 05:55:24.920: E/AndroidRuntime(1324): at java.lang.reflect.Method.invoke(Method.java:515)
04-18 05:55:24.920: E/AndroidRuntime(1324): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-18 05:55:24.920: E/AndroidRuntime(1324): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-18 05:55:24.920: E/AndroidRuntime(1324): at dalvik.system.NativeStart.main(Native Method)
04-18 05:55:38.600: I/Process(1324): Sending signal. PID: 1324 SIG: 9
答案 0 :(得分:1)
由于您已取得Void
进度,因此您需要String
,因为您期望String
进展。
答案 1 :(得分:1)
改变这个:
private class ChartTask extends AsyncTask< String,Void, Void>{
到此:
private class ChartTask extends AsyncTask<String, String, Void>{