我在Android studio中编写了一个代码来使用Web服务,如果返回“True”,则跳转到新活动。但是当我点击Login时,应用程序会一直崩溃。我是android studio的新手,我不知道我使用的过程是否正确。
网址 - http://202.54.216.49/logs/test.asmx
Method-loginauth
注意:如果用户名和密码正确,则loginauth返回“True”,如果不正确,则返回“False”。如果返回“True”,我想跳转到新活动(Main2Activity)。
MainActivity.java -
package com.example.abhimanyu.devicecontrol;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
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 AppCompatActivity {
EditText textBox, textBox2;
Button button;
TextView ans;
String URL = "http://202.54.216.49/logs/test.asmx";
String NAMESPACE = "http://tempuri.org/";
String SOAP_ACTION = "http://tempuri.org/loginauth";
String METHOD_NAME = "loginauth";
String PARAMETER_NAME1 = "username";
String PARAMETER_NAME2 = "password";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textBox = (EditText) findViewById(R.id.txtBox);
textBox2 = (EditText) findViewById(R.id.txtBox2);
button = (Button) findViewById(R.id.btn);
ans = (TextView) findViewById(R.id.answer);
final Context context;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CallWebService().execute(textBox.getText().toString(), textBox2.getText().toString());
}
});
}
class CallWebService extends AsyncTask<String, Void, String> {
@Override
protected void onPostExecute(String s) {
if (s.equalsIgnoreCase("True"))
{
Intent k = new Intent(MainActivity.this, Main2Activity.class);
startActivity(k);
}
else
{
Toast.makeText(getApplicationContext(), "Wrong username or password", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onPreExecute() {
Log.i("onPreexecute","running");
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String result = null;
SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo propertyInfo1 = new PropertyInfo();
propertyInfo1.setName(PARAMETER_NAME1);
propertyInfo1.setValue(params[0]);
propertyInfo1.setType(String.class);
soapObject.addProperty(propertyInfo1);
PropertyInfo propertyInfo2 = new PropertyInfo();
propertyInfo2.setName(PARAMETER_NAME2);
propertyInfo2.setValue(params[1]);
propertyInfo2.setType(String.class);
soapObject.addProperty(propertyInfo2);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.implicitTypes=true;
envelope.setOutputSoapObject(soapObject);
HttpTransportSE httpTransportSE = new HttpTransportSE(URL,30000);
try {
httpTransportSE.call(SOAP_ACTION, envelope);
SoapPrimitive soapPrimitive = (SoapPrimitive)envelope.getResponse();
result = soapPrimitive.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
}
Main2Activity.java -
package com.example.abhimanyu.devicecontrol;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
的AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abhimanyu.devicecontrol">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
<activity android:name=".Main2Activity"></activity>
</application>
</manifest>
注意:它显示exception- java.net.SocketTimeoutException
答案 0 :(得分:0)
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
将这些行添加到清单文件
后可以正常工作