【问题标题】:Post to asp.net web api via Android studio (migrate code from react)通过 Android Studio 发布到 asp.net web api(从 react 迁移代码)
【发布时间】:2018-09-27 07:40:22
【问题描述】:

我有一个 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?

【问题讨论】:

    标签: android-studio post react-native asp.net-web-api fetch


    【解决方案1】:
        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());
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2014-03-15
      • 2015-10-02
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      • 2019-01-11
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多