我正在尝试使用AsyncTask运行Android代码。然而,它导致了许多例外。我无法删除这些例外情况。应用程序意外停止。
有人可以帮助我吗?
这是我的Android代码:
package com.example;
import java.net.SocketException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class TeacherLoginActivity extends Activity
{
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://10.0.2.2/loginteacher/Service1.asmx";
String t_id;
String password;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button signin = (Button) findViewById(R.id.regsubmitbtn);
signin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText etxt_user = (EditText) findViewById(R.id.usereditlog);
t_id = etxt_user.getText().toString();
EditText etxt_password = (EditText) findViewById(R.id.pwdeditlog);
password = etxt_password.getText().toString();
new LoginTask().execute();
}
});
}
private String doLogin(String t_id, String password) {
String res=null;
final String SOAP_ACTION = "http://tempuri.org/GetLogin";
final String METHOD_NAME = "GetLogin";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("teacherid", t_id);
request.addProperty("password",password);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true; // Set this variable to true for
// compatibility with what seems to be the
// default encoding for .Net-Services.
envelope.setOutputSoapObject(request);
System.out.println(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Log.i("myApp", response.toString());
System.out.println("response" +response);
res=response.toString();
System.out.println(res);
}catch(SocketException ex)
{
Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
ex.printStackTrace();
}
catch (Exception e) {
Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
e.printStackTrace();
}
return res;
}
private class LoginTask extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(
TeacherLoginActivity.this);
protected void onPreExecute() {
this.dialog.setMessage("Logging in...");
this.dialog.show();
}
protected Void doInBackground(final Void... unused) {
String auth=doLogin(t_id,password);
System.out.println(auth);
return null; // don't interact with the ui!
}
protected void onPostExecute(Void result)
{
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
}
My logcat :
10-27 20:03:11.329: ERROR/Error :(493): Error on soapPrimitiveData() Permission denied
10-27 20:14:31.458: ERROR/Error :(576): Error on soapPrimitiveData() Permission denied
10-27 20:14:31.458: WARN/System.err(576): java.net.SocketException: Permission denied
10-27 20:14:31.498: WARN/System.err(576): at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
10-27 20:14:31.498: WARN/System.err(576): at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:186)
10-27 20:14:31.498: WARN/System.err(576): at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:265)
10-27 20:14:31.498: WARN/System.err(576): at java.net.Socket.checkClosedAndCreate(Socket.java:873)
10-27 20:14:31.498: WARN/System.err(576): at java.net.Socket.connect(Socket.java:1020)
10-27 20:14:31.498: WARN/System.err(576): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
10-27 20:14:31.498: WARN/System.err(576): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
10-27 20:14:32.869: ERROR/WindowManager(576): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-27 20:14:32.869: ERROR/WindowManager(576): at com.and roid.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-27 20:14:32.869: ERROR/WindowManager(576): at dalvik.system.NativeStart.main(Native Method)
10-27 20:14:34.188: INFO/Process(576): Sending signal. PID: 576 SIG: 9
答案 0 :(得分:2)
您可能需要在XML文件中添加权限。
类似的东西:
<uses-permission
android:name="android.permission.INTERNET" />