gpt4 book ai didi

php - 改造 2 : NO Request Body even there is

转载 作者:太空狗 更新时间:2023-10-29 14:41:12 25 4
gpt4 key购买 nike

你遇到过这个问题吗?我用 retrofit2 发出了一个 Post 请求,但请求正文不会提供给服务器。上次我检查提要时它是空的,这就是它无法通过 IF 语句的原因。这是我的代码:

 Gson gson = new GsonBuilder()
.setLenient()
.create();

Retrofit newAccountRetro = new Retrofit.Builder()
.baseUrl("http://192.168.1.8/waterdistrict/")
.addConverterFactory(GsonConverterFactory.create(gson))
.build();

RetrofitAPI retroAPI = newAccountRetro.create(RetrofitAPI.class);

我的请求体:

    NewFormRequest nfr = new NewFormRequest();
nfr.setFname(fname);
nfr.setMname(mname);
nfr.setLname(lname);
nfr.setUsern(usern);
nfr.setPs(ps);
nfr.setContact(contact);
nfr.setAdd(add);
nfr.setEmail(email);

rs = new Presenter(context);
//TODO:Fix null response of Retrofit

这就是我传递请求正文的方式

   Call<NewFormResponse> nfResponse = retroAPI.newAccount((NewFormRequest) nfr);
nfResponse.enqueue(new Callback<NewFormResponse>() {
@Override
public void onResponse(Call<NewFormResponse> call, Response<NewFormResponse> response) {
Log.d(getClass().toString(), response.code()+"-"+new Gson().toJson(response.body())
+"-"+call.request().toString()+" - "+call.request().headers().toString());
if(response.code() == 200){
rs.nfResponse(response.body().getCode(), response.body().getMessage());
rs.switchProgress();
}
};

@Override
public void onFailure(Call<NewFormResponse> call, Throwable t) {
rs.switchProgress();
Log.d(getClass().toString(), t.toString());

}
});

终点:

 @POST("addClients.php")
Call<NewFormResponse> newAccount (@Body NewFormRequest newFormRequest);

新表单响应:

public class NewFormResponse {
@SerializedName("code")
@Expose
private Integer code;
@SerializedName("message")
@Expose
private String message;

public Integer getCode() {
return code;
}

public void setCode(Integer code) {
this.code = code;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}

newFormRequest 类

public class NewFormRequest {
private String fname;
private String mname;
private String lname;
private String usern;
private String ps;
private String contact;
private String add;
private String email;

public String getFname() {
return fname;
}

public void setFname(String fname) {
this.fname = fname;
}

public String getMname() {
return mname;
}

public void setMname(String mname) {
this.mname = mname;
}

public String getLname() {
return lname;
}

public void setLname(String lname) {
this.lname = lname;
}

public String getUsern() {
return usern;
}

public void setUsern(String usern) {
this.usern = usern;
}

public String getPs() {
return ps;
}

public void setPs(String ps) {
this.ps = ps;
}

public String getContact() {
return contact;
}

public void setContact(String contact) {
this.contact = contact;
}

public String getAdd() {
return add;
}

public void setAdd(String add) {
this.add = add;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}

PHP代码

<?php
include "connection.php";

if(mysqli_connect_errno()){
$response["code"] = 0;
$response["message"] = "failed to connect with the database";
echo json_encode($response);
} else{
if(isset($_POST['fname'], $_POST['mname'], $_POST['lname'], $_POST['usern'], $_POST['ps'], $_POST['contact'], $_POST['add'], $_POST['email'])){
$fname = $_POST['fname'];
$mname = $_POST['mname'];
$lname = $_POST['lname'];
$contactnum = $_POST['contact'];
$address = $_POST['add'];
$email = $_POST['email'];
$usern = $_POST['usern'];
$password = $_POST['ps'];
$approval = 0;

try{
$conni = new PDO("mysql:host=localhost;dbname=wddatabase","root","");
$conni->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql_insert = "INSERT INTO clients (fname, mname, lname, uname, password, contact_num, address, email, approved)
VALUES ('$fname', '$mname', '$lname', '$usern', '$password', '$contactnum', '$address', '$email', '$approval')";
$conni->exec($sql_insert);
$response['code']= 1;
$response['message']='New Record Created';
echo json_encode($response);
}catch(PDOException $e){
$response['code']= 0;
$response['message']=$e;
echo json_encode($response);
}
} else{
$response['code']= 0;
$response['message']="Error";
echo json_encode($response);
}
}

?>

//结果

200-{"code":0,"message":"Error"}-Request{method=POST, url=http://192.168.1.8/waterdistrict/addClients.php, tag=null} - 

最佳答案

在 php 中你应该在回显 json 结果之前将 header 设置为 json:

 header('Content-Type: application/json');
echo $json;

关于php - 改造 2 : NO Request Body even there is,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47951136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com