我在类中有一个字符串,我想从另一个类访问。
我希望我的responseString
课程中可以访问字符串result
或onPostExecute()
(来自Registration.java
)。
以下是RequestTask.java
,其中包含我需要的字符串:
@SuppressLint("NewApi")
class RequestTask extends AsyncTask<String, String, String>{
//needed string
String responseString = null;
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String responseString = null;
responseString = out.toString();
Log.d("check response", responseString);
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
public void onPostExecute(String result) {
super.onPostExecute(result);
}
}
以下是Registration.java
,我希望字符串显示在最底部的else
语句中:
@SuppressLint("NewApi")
public class Registration extends Activity {
String result2 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
TextView detail = (TextView)findViewById(R.id.resulttext);
detail.setText(result2);
}
@SuppressLint("NewApi")
public void SubmitRegistration(View view) {
// assign text in fields to string values
EditText first = (EditText)findViewById(R.id.first);
String first2 = first.getText().toString();
EditText last = (EditText)findViewById(R.id.last);
String last2 = last.getText().toString();
EditText display = (EditText)findViewById(R.id.display);
String display2 = display.getText().toString();
//calculates the number of characters in the display field
int length2 = display2.length();
EditText email = (EditText)findViewById(R.id.email);
String email2 = email.getText().toString();
EditText password = (EditText)findViewById(R.id.password);
String password2 = password.getText().toString();
EditText vpassword = (EditText)findViewById(R.id.vpassword);
String vpassword2 = vpassword.getText().toString();
//calculates the number of characters in the password field
int length = vpassword2.length();
// verifying the following in order: Passwords match? A Password field is empty?
//Password and Display Name less than 6 characters long? Email contains an @ sign and a period?
if(!vpassword2.equals(password2))
{
Toast.makeText(getApplicationContext(), "Passwords do not match!", Toast.LENGTH_SHORT).show();
}
else if (password2.isEmpty() || vpassword2.isEmpty()){
Toast.makeText(getApplicationContext(), "Password field is empty", Toast.LENGTH_SHORT).show();
}
else if (length < 6 || length2 < 6 ) {
Toast.makeText(getApplicationContext(), "Password and Display Name must be at least 6 characters long", Toast.LENGTH_LONG).show();
}
else if (!email2.contains("@") || !email2.contains(".")){
Toast.makeText(getApplicationContext(), "Must enter valid email address.", Toast.LENGTH_SHORT).show();
}
else {
String asd = "http://www.alkouri.com/android/registercheck.php?first=" + first2 + "&last=" + last2 + "&dispname=" + display2 + "&email=" + email2 + "&password=" + password2;
Toast.makeText(getApplicationContext(), froyo, Toast.LENGTH_LONG).show();
//send php with all the data to server for validation and insertion into table
new RequestTask().execute("http://www.alkouri.com/android/registercheck.php?first=" + first2 + "&last=" + last2 + "&display=" + display2 + "&email=" + email2 + "&password=" + password2 );
}
}
}
我已尝试将其设为public void
但我仍然无法在Registration.java
答案 0 :(得分:0)
您是否尝试从请求任务覆盖onPostExecute(String result)方法:
样品:
String asd = "http://www.alkouri.com/android/registercheck.php?first=" + first2 + "&last=" + last2 + "&dispname=" + display2 + "&email=" + email2 + "&password=" + password2;
Toast.makeText(getApplicationContext(), froyo, Toast.LENGTH_LONG).show();
//send php with all the data to server for validation and insertion into table
RequestTast task = new RequestTask()
{
@override
public void onPostExecute(String result) {
... do something with the resut..
}
}
task.execute("http://www.alkouri.com/android/registercheck.php?first=" + first2 + "&last=" + last2 + "&display=" + display2 + "&email=" + email2 + "&password=" + password2 );
答案 1 :(得分:0)
在onPostExecute(String arg)
你可以开始你的活动
@SuppressLint("NewApi")
class RequestTask extends AsyncTask<String, String, String>{
//needed string
String responseString = null;
private Context mContext;
//This is where you pass the context
public RequestTask(Context context) {
mContext = context;
}
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String responseString = null;
responseString = out.toString();
Log.d("check response", responseString);
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
public void onPostExecute(String result) {
super.onPostExecute(result);
Intent i = new Intent(mContext Registration.class);
Bundle bundle = new Bundle();
bundle.putString("REGISTRATION", result);
i.putExtras(i);
mContext.startActivity(i);
}
}
然后,在您的注册类中执行此操作:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
String myResult = (String) b.getString("REGISTRATION");
setContentView(R.layout.register);
TextView detail = (TextView)findViewById(R.id.resulttext);
detail.setText(result2);
}
最后,当您调用RequestTaskt类时,您将传递Context through参数,例如RequestTask mTask = new RequestTask(getActivity());