【发布时间】:2015-03-09 19:16:46
【问题描述】:
我在调用 rest api 时遇到了一些麻烦。使用 Chrome Advanced Rest Client 时,我使用 URL:http://example.com/task_manager/v1/register 进行测试,效果很好。我试图让用户能够通过 android 注册。当我单击我的按钮时,我没有看到对数据库的任何更改。
我是通过xml调用方法android:onClick="register"
public class LoginActivity extends Activity {
public EditText enterEmail;
public EditText enterPassword;
public EditText forgotPassword;
public ImageButton loginButton;
public ImageButton RegisterButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//hide status bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login_activity);
enterEmail = (EditText) findViewById(R.id.emailentry);
enterPassword = (EditText) findViewById(R.id.passwordentry);
forgotPassword = (EditText) findViewById(R.id.forgotpass);
//login
loginButton = (ImageButton) findViewById(R.id.loginbutton);
//register
RegisterButton = (ImageButton) findViewById(R.id.registerbutton);
}
public void login(View view){
}
public void register(View view){
String email = enterEmail.getText().toString();
String password = enterPassword.getText().toString();
String name = "ANDROID";
String apiURL = "http://example.com/task_manager/v1/register";
if( (email != null && !email.isEmpty()) && (password != null && !password.isEmpty()) && (name != null && !name.isEmpty())) {
String urlString = apiURL + "name="+name+"&email="+email+"&password="+password;
new CallAPI().execute(urlString);
}
}
private class CallAPI extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String urlString=params[0]; // URL to call
String resultToDisplay = "";
InputStream in = null;
// HTTP Get
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream());
} catch (Exception e ) {
System.out.println(e.getMessage());
return e.getMessage();
}
return resultToDisplay;
}
protected void onPostExecute(String result) {
}
} // end CallAPI
}
【问题讨论】: