如何 post 数据从正文 android 中恢复 api JSON c#
how to post data to rest api JSON from Body android c#
谁能帮我找出解决办法?我需要使用 POST 方法注册数据 从 Body.I 发送参数 我正在使用此代码
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 个内部服务器 Error.Please 帮助我。
你可以这样使用它:
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);
}
}
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;
}
试试这个,它会起作用
谁能帮我找出解决办法?我需要使用 POST 方法注册数据 从 Body.I 发送参数 我正在使用此代码
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 个内部服务器 Error.Please 帮助我。
你可以这样使用它:
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);
}
}
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;
}
试试这个,它会起作用