通过 Android Studio 发布到 asp.net web api(从 react 迁移代码)



我有一个 asp.net 的Web api:

public virtual async Task<Customer> AddCustomer(Customer customer, CancellationToken cancellation)
        {
            Customer existingCustomer = (await Repository.GetAllAsync(cancellation)).Where(cu => cu.NationalCode == customer.NationalCode).FirstOrDefault();
            if (existingCustomer != null)
                throw new Exception("you registered before. please login");
            return await Repository.AddAsync(customer, cancellation);
        }

我可以通过以下代码在反应本机中发布到此 api:

var url = '/Sanaap.Api/odata/Sanaap/Customers/Sanaap.LoginCustomer';
        fetch(url, {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                FirstName: '',
                LastName: '',
                NationalCode: this.state.ncode.toString(),
                Mobile: this.state.mobile.toString(),
            })
        }).then((response) => {
            if (response.status == 500) {
                alert('500');
                Toast.show({
                    text: "not found",
                    position: 'bottom', duration: 5000, type: 'danger'
                });
                return;
            }
            if (response.status == 200) {
                alert('200');
                this.props.navigation.navigate("EvalReq");
            }
        });

什么是 Android Studio 中的相等代码,而不是获取发布客户并获取插入的客户 ID?

    protected String doInBackground(String... params) {
        // TODO: attempt authentication against a network service.
        progress.setVisibility(View.VISIBLE);
        try {
            URL url = new URL("http://192.168.10.112/Sanaap.Api/odata/Sanaap/Customers/Sanaap.AddCustomer");
            JSONObject json = new JSONObject();
            json.put("FirstName", mfname);
            json.put("LastName", mlname);
            json.put("NationalCode", mncode);
            json.put("Mobile", mmobile);
            Log.e("params",json.toString());
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Accept","application/json");
            conn.setRequestProperty("Content-Type","application/json; charset=utf-8");
            //conn.setDoInput(true);
            conn.setDoOutput(true);
            DataOutputStream os = new DataOutputStream(conn.getOutputStream());
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(json.toString());
            Log.e("String.valueOf(json) : ",String.valueOf(json));
            writer.flush();
            writer.close();
            os.close();
            int responseCode=conn.getResponseCode();
            if (responseCode == HttpsURLConnection.HTTP_OK) {
                BufferedReader in=new BufferedReader(new
                        InputStreamReader(
                        conn.getInputStream()));
                StringBuffer sb = new StringBuffer("");
                String line="";
                while((line = in.readLine()) != null) {
                    sb.append(line);
                    break;
                }
                in.close();
                return sb.toString();
            }
            else {
                Toast.makeText(FirstActivity.this, "Problem in post.", Toast.LENGTH_SHORT).show();
                return new String("false : "+responseCode);
            }
        }
        catch(Exception e){
            return new String("Exception: " + e.getMessage());
        }
    }

最新更新