iT邦幫忙

0

請問Android studio 連接asp.net web api 的 post method

  • 分享至 

  • xImage

大家好!
因為作業碰到了些問題,花了很多時間仍然卡關,希望各位高手能幫幫我解決問題
以下是手機部分:
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;

http://ithelp.ithome.com.tw/upload/images/20170907/20106717VM1wbDstBO.png
但資料庫卻未出現我所建入的資料,我使用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測試,並順利建入資料
請問到底是哪裡出了問題呢?

看更多先前的討論...收起先前的討論...
黃彥儒 iT邦高手 1 級 ‧ 2017-09-06 23:33:50 檢舉
建議程式碼部分用複製的,不要截圖哦
丹尼 iT邦研究生 4 級 ‧ 2017-09-07 00:11:36 檢舉
打廣告嫌疑唷
http://ithelp.ithome.com.tw/articles/10159270
Android Device Monitor-->LogCat
哪裡出問題看這邊就知道了
e729655 iT邦新手 5 級 ‧ 2017-09-07 13:42:37 檢舉
好的!我在編輯
沒有打廣告啦!這是一堂課的作業
好,會會去看看,感恩
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答