【问题标题】:how to post data to rest api JSON from Body android c#如何从 Body android c# 将数据发布到 rest api JSON
【发布时间】:2020-10-03 12:07:09
【问题描述】:

谁能帮我找出解决办法?我需要使用 POST 方法注册数据,从 Body 发送参数。我正在使用此代码

  public static async Task<HttpResponseMessage> Signup(Register register)
    {
        HttpResponseMessage result=null;
        try
        {
            string url = Constant.url + "api/services/app/Account/Register";
            HttpClient _client = new HttpClient();
            var content = JsonConvert.SerializeObject(register);
            result = await _client.PostAsync(url, new StringContent(content, Encoding.UTF32, "application/json"));
        }
        catch(Exception ex)
        {
        }
            return result;
    }

但我收到 500 内部服务器错误。请帮助我。

【问题讨论】:

  • 检查您的 api 代码和 api 错误日志以获取更多信息
  • 在不知道您向服务器发送什么以及服务器期望什么的情况下,没有人可以帮助您解决问题。 500 也表示服务器有问题。所以你必须检查和服务器的代码,看看有什么问题

标签: android android-studio xamarin.android http-post webapi


【解决方案1】:
  public static  Signup_Root Check_xsignup(Register register)
    {
        HttpResponseMessage result = null;
        string resultt = "";
        Signup_Root lp =null;
        try
        {
            string jObj = JsonConvert.SerializeObject(register);
            StringContent content = new StringContent(jObj, System.Text.Encoding.UTF8);
            MediaTypeHeaderValue mValue = new MediaTypeHeaderValue("application/json");
            content.Headers.ContentType = mValue;
            string url = Constant.url + "api/services/app/Account/Register";
            HttpClient _client = new HttpClient();
            result =  _client.PostAsync(url, content).Result;
            if (result.StatusCode == HttpStatusCode.OK)
            {
                resultt = result.Content.ReadAsStringAsync().Result;
                lp = JsonConvert.DeserializeObject<Signup_Root>(resultt);
            }
        }
        catch (Java.Lang.Exception ex)
        {

        }
        return lp;
    }

试试这个,它会工作的

【讨论】:

    【解决方案2】:

    你可以这样使用它:

    public class MakeRequest extends AsyncTask<String, Void, JSONObject> {
    
            protected void onPreExecute(){ }
    
            protected JSONObject doInBackground(String... arg0) {
    
                try {
                    URL url = new URL(AppConstants.DEMO_API); // here is your URL path
                    //params....
                    JSONObject postDataParams = new JSONObject();
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("type","claim");
                    postDataParams.put("data", jsonObject);
                    Log.e(TAG, postDataParams.toString());
                    postDataParams.put("ctx","j");
                    postDataParams.put("email", "abc@gmail.com");
    
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setReadTimeout(60000 /* milliseconds */);
                    conn.setConnectTimeout(60000 /* milliseconds */);
                    conn.setRequestMethod("POST"/* meathod */);
                    conn.setDoInput(true);
                    conn.setDoOutput(true);
    
                    OutputStream os = conn.getOutputStream();
                    BufferedWriter writer = new BufferedWriter(
                            new OutputStreamWriter(os, "UTF-8"));
                    writer.write(getPostDataString(postDataParams));
    
                    writer.flush();
                    writer.close();
                    os.close();
    
                    //response....
                    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();
    
                        // getting JSON string from URL
                        JSONObject json = new JSONObject(sb.toString());
                        return json;
                    }
                    else {
                        return new JSONObject();
                    }
                }
                catch(Exception e){
                    e.printStackTrace();
    
                    return new JSONObject();
                }
    
            }
    
            @Override
            protected void onPostExecute(JSONObject jsonObject) {
                super.onPostExecute(jsonObject);
                
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2015-10-08
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 2023-03-05
      • 2018-06-30
      • 2011-02-27
      相关资源
      最近更新 更多