嗨,我有RegisterActivity.java
这样的:
public class RegisterActivity extends Activity{
private static final String TAG = "PostFetcher";
private static String URL = "http://api.example.com/";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
final EditText inputFname = (EditText) findViewById(R.id.registerFname);
final EditText inputLname = (EditText) findViewById(R.id.registerLname);
final EditText inputEmail = (EditText) findViewById(R.id.registerEmail);
Button btnRegister = (Button) findViewById(R.id.btnRegister);
Button btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLogin);
final TextView loginErrorMsg = (TextView) findViewById(R.id.register_error);
// Register Button Click event
btnRegister.setOnClickListener(new View.OnClickListener() {
Login login2;
RadioGroup radioSexGroup = (RadioGroup) findViewById(R.id.sex);
public void onClick(View view) {
String fname = inputFname.getText().toString();
String lname = inputLname.getText().toString();
String email = inputEmail.getText().toString();
// get selected radio button from radioGroup
int selectedId = radioSexGroup.getCheckedRadioButtonId();
RadioButton radioSexButton = (RadioButton) findViewById(selectedId);
String gender = radioSexButton.getText().toString();
//System.out.println(fname);
//Toast.makeText(RegisterActivity.this, radioSexButton.getText(), Toast.LENGTH_SHORT).show();
String registerURL = URL +"&user_email="+ email /**+"&first_name="+ fname +"&last_name="+ lname*/ +"&gender="+ gender;
System.out.println(registerURL);
if( email.length() == 0) {
loginErrorMsg.setText(R.string.empty);
//Toast.makeText(view.getContext(), R.string.empty, Toast.LENGTH_SHORT).show();
return;
}else{
try {
//Create an HTTP client
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(registerURL);
//Perform the request and check the status code
HttpResponse response = client.execute(post);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
try {
//Read the server response and attempt to parse it as JSON
Reader reader = new InputStreamReader(content);
Gson gson = new Gson();
this.login2 = gson.fromJson(reader, Login.class);
//System.out.println(this.login2);
//handlePostsList(posts);
} catch (Exception ex) {
Log.e(TAG, "Failed to parse JSON due to: " + ex);
failedLoading();
}
} else {
Log.e(TAG, "Server responded with status code: " + statusLine.getStatusCode());
failedLoading();
}
} catch(Exception ex) {
Log.e(TAG, "Failed to send HTTP POST request due to: " + ex);
failedLoading();
}
//To set register message
if(login2.getResult().equals("OK")){
loginErrorMsg.setText(login2.getMessage().toString());
}else if(login2.getResult().equals("KO")){
loginErrorMsg.setText(login2.getMessage().toString());
}
}
}
});
// Link to Login
btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),LoginActivity.class);
startActivity(i);
finish();
}
});
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.male:
if (checked)
// Pirates are the best
break;
case R.id.female:
if (checked)
// Ninjas rule
break;
}
}
private void failedLoading() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(RegisterActivity.this, "Failed to Register. look at LogCat.", Toast.LENGTH_SHORT).show();
}
});
}
}
但是我收到如下错误:Failed to send HTTP POST request due to: android.os.NetworkOnMainThreadException
Android开发者论坛建议我使用它AsyncTask
来解决这个问题。但我不知道如何改变这一点。有人可以帮我解决这个问题吗?我花了几个小时,但找不到任何解决方案。