大家好!
因為作業碰到了些問題,花了很多時間仍然卡關,希望各位高手能幫幫我解決問題
以下是手機部分:
private static final String URL = "http://localhost:27043/api/UserApi/Regist";
private class DoCreateAccount extends AsyncTask<String,Void,Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(getApplicationContext(),"送出資料!",Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(String... voids) {
String json_data = voids[0];
try {
java.net.URL url = new URL(URL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.connect();
OutputStream Output = httpURLConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(Output, "UTF-8"));
writer.write(json_data);
writer.flush();
writer.close();
Output.close();
InputStream is = httpURLConnection.getInputStream();
String result = "";
int byteCharacter;
while ((byteCharacter = is.read()) != -1){
result += (char) byteCharacter;
}
Log.d("json api","DoCreateAccount.doInBackground Json return " + result);
is.close();
httpURLConnection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
case R.id.signupbtn6:
account = new Account(start_id.getText().toString(),start_mail.getText().toString()
,start_nn.getText().toString(),start_phone.getText().toString(),start_pw.getText().toString()
,start_pw2.getText().toString());
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("Student_ID", account.getStudent_ID());
jsonObject.put("AccountEmail", account.getAccountEmail());
jsonObject.put("User_Nickname", account.getUser_Nickname());
jsonObject.put("User_Password",account.getUser_Password());
jsonObject.put("User_Phone",account.getUser_Phone());
jsonObject.put("Comfirm_User_Password",account.getComfirm_User_Password());
json_data1 = jsonObject.toString();
Toast.makeText(getApplicationContext(),json_data1,Toast.LENGTH_LONG).show();
new DoCreateAccount().execute(json_data1);
} catch (JSONException e) {
e.printStackTrace();
}
finish();
break;
但資料庫卻未出現我所建入的資料,我使用sql server
以下是web api
[HttpPost, ActionName("Regist")]
public HttpResponseMessage PostRegistration([FromBody] User user)
{
user.ActivationCode = Guid.NewGuid();
user.IsEmailVerified = false;
user.MemberStatus = 1;
try
{
using (FOODEntities db = new FOODEntities())
{
db.User.Add(user);
db.SaveChanges();
var message = Request.CreateResponse(HttpStatusCode.Created, user);
message.Headers.Location = new Uri(Request.RequestUri + user.User_ID.ToString());
return message;
}
}
catch (Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, e);
}
}
這部分有使用postman測試,並順利建入資料
請問到底是哪裡出了問題呢?