本文介绍了我无法从onPostExecute价值传递到另一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有波纹code和我需要传递 onPostExecute
的值到另一个意图
,但它让我崩溃
。下面是 Logins.class
,我需要 onPostExecute
的值传递给 MainActivity.class
:
I have bellow code and i need to pass value of onPostExecute
to another with intent
but it get me crash
. below is Logins.class
that i need to pass value of onPostExecute
to MainActivity.class
:
public class Logins extends Activity {
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "Login";
String SOAP_ACTION = "http://tempuri.org/Login";
String URL = "http://test.com/MobileWebService/Related.asmx";
public String Webresponse = "IS NULL ?";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyLogin task = new MyLogin();
task.execute(new String[] { URL });
}
public String[] test(String user,String pass)
{
String[] stvalue = {user,pass};
new MyLogin().execute(stvalue);
return stvalue;
}
private class MyLogin extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... urls) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("username", urls[0]);
request.addProperty("pass", urls[1]);
request.addProperty("device", "0");
request.addProperty("security", 128110);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setAddAdornments(false);
envelope.implicitTypes = true;
envelope.setOutputSoapObject(request);
HttpTransportSE conn = new HttpTransportSE(URL);
conn.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
try {
conn.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope
.getResponse();
Webresponse = response.toString();
Log.i("LOG", Webresponse);
}
catch (Exception e) {
e.printStackTrace();
Log.i("xxx", e.toString());
}
return Webresponse;
}
@Override
protected void onPostExecute(String result) {
// someMethod(result);
Intent intent = new Intent(Logins.this, MainActivity.class);
intent.putExtra("EXTRA_SESSION_ID", result);
startActivity(intent);
}
}
}
这是我的 Activity.class
:
public class MainActivity extends Activity {
EditText UserName;
EditText PassWord;
Button Login;
String mUserName;
String mPassWord;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UserName = (EditText) findViewById(R.id.edtUserName);
PassWord = (EditText) findViewById(R.id.edtPasswprd);
Login = (Button) findViewById(R.id.btnLogin);
Login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
mUserName = UserName.getText().toString().trim();
mPassWord = PassWord.getText().toString().trim();
if (mUserName.length() > 0 && mPassWord.length() > 0) {
Logins lt = new Logins();
lt.test(mUserName, mPassWord);
---I need get value from here--->Bundle bundle = getIntent().getExtras();---BUT CRASH----
String uId = bundle.getString("EXTRA_SESSION_ID");
Log.i("OK", uId);
} else {
Toast.makeText(
G.context,
G.context.getResources().getString(
R.string.btn_LoginError), Toast.LENGTH_LONG)
.show();
}
}
});
}
}
我的 LogCat中
是:
AndroidRuntime
at com.test.MainActivity$1.onClick(MainActivity.java:53)
推荐答案
您应该与接口
在的AsyncTask $ C解决此$ C>:
AsyncTask的:
AsyncTask:
public class MyAsyncTask extends AsyncTask<Void, Void, Response> {
public interface GetMyTaskListener {
public void onGetMyTaskComplete(Response response);
}
Request request;
GetMyTaskListener listener;
public MyAsyncTask(Request request,
GetMyTaskListener listener) {
this.request = request;
this.listener = listener;
}
protected void onPostExecute(Response result) {
listener.onGetMyTaskComplete(result);
}
@Override
protected Response doInBackground(Void... params) {
...
return response;
}
}
活动:
public class MyActivity extends Activity implements GetMyTaskListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
Request req = new Request();
new MyAsyncTask(req, this).execute();
}
@Override
public void onGetMyTaskComplete(
Response response) {
//TODO use the information from the AsyncTask
}
}
这篇关于我无法从onPostExecute价值传递到另一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!