我是android开发的新手。我正在使用android studio
来开发应用。 我知道有很多与之相关的问题,我调查了它们但我的问题仍然存在。以下是我研究过的解决方案。
我清楚地调查了它们并与我的问题相匹配。但我仍然无法解决我的问题。
该应用很简单,会有editText
个button
和textView
。用户应在id
中输入editText
,当点按button
时,应显示相应的name
。该按钮将使用webservice
。
为此,我在SQL
中创建了一些表格。并在webservice
中创建了dotnet
。
以下是我的网络服务
[WebMethod]
public string findContact(string eid)
{
return getContact(eid);
}
public string getContact(string eid)
{
SqlConnection conn;
conn = ConnectionManager.GetConnection();
conn.Open();
SqlCommand newCmd = conn.CreateCommand();
newCmd.CommandType = CommandType.Text;
newCmd.CommandText = "Select Name from Users where Id=" + eid + "";
SqlDataReader sdr = newCmd.ExecuteReader();
String address = null;
if(sdr.Read())
{
address = sdr.GetValue(0).ToString();
}
conn.Close();
return address;
}
以下是我的清单文件
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
以下是我的java代码
public class MainActivity extends AppCompatActivity {
/** Called when the activity is first created. */
private static final String SOAP_ACTION = "http://tempuri.org/findContact";
private static final String METHOD_NAME = "findContact";// webservice web method name
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://192.168.100.9:9698/WebService.asmx";
TextView textView;
Button button;
EditText editText;
String ID;
public static final String LOG_TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.webServ);
button = (Button)findViewById(R.id.btnSubmit);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
PropertyInfo propertyInfo = new PropertyInfo();
propertyInfo.type = PropertyInfo.STRING_CLASS;
propertyInfo.name = "eid";
editText = (EditText)findViewById(R.id.enterID);
ID = editText.getText().toString();
request.addProperty(propertyInfo,ID);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransportSE = new HttpTransportSE(URL);
httpTransportSE.debug = true;
try
{
httpTransportSE.call(SOAP_ACTION,envelope);
Object response = envelope.getResponse();
textView.setText(response.toString());
} catch (XmlPullParserException e) {
textView.setText(e.toString() + " Or enter ID is not valid!");
e.printStackTrace();
} catch (IOException e) {
textView.setText(e.toString() + " IOException Or enter ID is not valid!");
e.printStackTrace();
}
String res = (httpTransportSE.responseDump).toString();
Log.v(LOG_TAG, res);
}
});
}}
当我点击按钮时,我收到了波纹管错误。
org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <HTML>@2:7 in java.io.InputStreamReader@960e1fa)
02-10 12:20:50.994 11713-11713/com.example.accurat.accesswebservice W/System.err: at org.kxml2.io.KXmlParser.require(KXmlParser.java:2065)
02-10 12:20:50.994 11713-11713/com.example.accurat.accesswebservice W/System.err: at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:127)
02-10 12:20:50.994 11713-11713/com.example.accurat.accesswebservice W/System.err: at org.ksoap2.transport.Transport.parseResponse(Transport.java:63)
02-10 12:20:50.994 11713-11713/com.example.accurat.accesswebservice W/System.err: at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:100)
02-10 12:20:50.994 11713-11713/com.example.accurat.accesswebservice W/System.err: at com.example.accurat.accesswebservice.MainActivity$1.onClick(MainActivity.java:75)
02-10 12:20:50.994 11713-11713/com.example.accurat.accesswebservice W/System.err: at android.view.View.performClick(View.java:4780)
02-10 12:20:50.994 11713-11713/com.example.accurat.accesswebservice W/System.err: at android.view.View$PerformClick.run(View.java:19866)
02-10 12:20:50.994 11713-11713/com.example.accurat.accesswebservice W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
02-10 12:20:50.994 11713-11713/com.example.accurat.accesswebservice W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
02-10 12:20:50.995 11713-11713/com.example.accurat.accesswebservice W/System.err: at android.os.Looper.loop(Looper.java:135)
02-10 12:20:50.995 11713-11713/com.example.accurat.accesswebservice W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
02-10 12:20:50.995 11713-11713/com.example.accurat.accesswebservice W/System.err: at java.lang.reflect.Method.invoke(Native Method)
02-10 12:20:50.995 11713-11713/com.example.accurat.accesswebservice W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
02-10 12:20:50.995 11713-11713/com.example.accurat.accesswebservice W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
02-10 12:20:50.995 11713-11713/com.example.accurat.accesswebservice W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
点击httpTransportSE.call(SOAP_ACTION,envelope);
我在模拟器上测试它,下面是我的模拟器的屏幕截图
我还检查了我在网络浏览器中运行的Webservice
,它工作正常。但它不在模拟器上。
任何帮助都将受到高度赞赏。