我正在尝试制作我的第一个Android网络服务,我在拨打简单电话时遇到问题。
我收到此处的错误androidHttpTransport.call(SOAP_ACTION, envelope)
欢迎任何帮助
以下是我的MainActivity.java
的enitre代码package com.teachingperiod.android.testwebservice;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class MainActivity extends ActionBarActivity {
private final String NAMESPACE = "http://mysite.sytes.net:9040/";
private final String URL = "http://mysite.sytes.net:9040/Math.asmx?";
private final String SOAP_ACTION = "http://mysite.sytes.net:9040/Math.asmx?op=InsertComment";
private final String METHOD_NAME = "InsertComment";
private String TAG = "PGGURU";
private static String celcius;
private static String fahren;
Button b;
TextView tv;
EditText et;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Celcius Edit Control
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View v) {
//Create request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Property which holds input parameters
PropertyInfo celsiusPI = new PropertyInfo();
//Set Name
celsiusPI.setName("NewComment");
//Set Value
celsiusPI.setValue("cell");
//Set dataType
celsiusPI.setType(String.class);
//Add the property to request object
request.addProperty(celsiusPI);
//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, envelope);
//Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Assign it to fahren static variable
fahren = response.toString();
} catch (Exception e) {
Log.i("wawa",e.getMessage());
e.printStackTrace();
e.getMessage();
}
}
}
这是错误日志
05-21 22:52:14.231 32163-32163/com.teachingperiod.android.testwebservice E/Trace﹕ error opening trace file: No such file or directory (2)
05-21 22:52:34.483 32163-32163/com.teachingperiod.android.testwebservice E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3609)
at android.view.View.performClick(View.java:4102)
at android.view.View$PerformClick.run(View.java:17063)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5454)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3604)
at android.view.View.performClick(View.java:4102)
at android.view.View$PerformClick.run(View.java:17063)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5454)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.i(Log.java:159)
at com.teachingperiod.android.testwebservice.MainActivity.onClick(MainActivity.java:99)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3604)
at android.view.View.performClick(View.java:4102)
at android.view.View$PerformClick.run(View.java:17063)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5454)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)
05-21 22:52:34.523 616-1066/? E/EmbeddedLogger﹕ App crashed! Process: com.teachingperiod.android.testwebservice
05-21 22:52:34.523 616-1066/? E/EmbeddedLogger﹕ App crashed! Package: com.teachingperiod.android.testwebservice v1 (1.0)
05-21 22:52:34.523 616-1066/? E/EmbeddedLogger﹕ Application Label: TestWebService
答案 0 :(得分:0)
第99行
Log.i("wawa",e.getMessage());
e.getMessage()返回一个空指针。将其更改为
Log.i("wawa",e.getMessage() + "");
确保不向Log类发送null。
答案 1 :(得分:0)
java.lang.IllegalStateException:无法执行活动的方法> http连接必须执行另一个线程(不是主线程)
public void onClick(View v)
{
new Thread() {
@Override
public void run() {
//Create request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
....
Log.i("wawa",e.getMessage());
e.printStackTrace();
e.getMessage();
}
}.start();
}