简体   繁体   English

改造如何使用改造将Json内部的Json转换为字符串

[英]Retrofit how to get a Json inside a Json to a string using Retrofit

How to return String data from the json inside a json. 如何从json中的json返回String数据。 One is a check to see if there is data. 一种是检查是否有数据。 a 201 response should then lead to the data being turned into a string. 然后201响应将导致数据变成字符串。

I have tried to the following but it does not work. 我尝试了以下操作,但不起作用。 What have I got wrong. 我怎么了 Can anyone explain to me so I can learn this? 谁能向我解释,以便我学习?

//the method I call on button press. //我按下按钮时调用的方法。
PhpResponse and getCervicalROM are the Model classes listed below. PhpResponse和getCervicalROM是下面列出的Model类。 I try to Toast to make sure I am getting the String variable data. 我尝试Toast以确保获取字符串变量数据。 But nothing comes up. 但是什么都没有发生。


 public void downloadromdata() {
    Call <PhpResponse> call = RetrofitClient.getInstance().getAPIService().getCervicalROM(ID);

    call.enqueue(new Callback<PhpResponse>() {
        @Override
        public void onResponse(Call<PhpResponse> call, Response<PhpResponse> response) {

            if(response.code() == 201)
            {
            PhpResponse phpResponse = response.body();
            RACERVICALROT = phpResponse.getUser().getRACERVICALROT();
            LACERVICALROT = phpResponse.getUser().getLACERVICALROT();
                Toast.makeText(cervicaltest.this, RACERVICALROT + "&" + LACERVICALROT, Toast.LENGTH_LONG).show();
        }else if(response.code() == 422){
                Toast.makeText(cervicaltest.this, "No data found", Toast.LENGTH_LONG).show();

            }
        }

        @Override
        public void onFailure(Call<PhpResponse> call, Throwable t) {
        }
    });
}

getCervicalROM model getCervicalROM模型

 public class getCervicalROM {

private String RACERVICALROT, LACERVICALROT;

public getCervicalROM(String RACERVICALROT, String LACERVICALROT) {
    this.RACERVICALROT = RACERVICALROT;
    this.LACERVICALROT = LACERVICALROT;
}
public String getRACERVICALROT() {
    return RACERVICALROT;
}
public String getLACERVICALROT() {
    return LACERVICALROT;
}

} }


Phpresponse Model 响应模型

 public class PhpResponse {

private boolean error;
private String message;
private getCervicalROM user;

public PhpResponse(boolean error, String message, getCervicalROM user) {
    this.error = error;
    this.message = message;
    this.user = user;
}

public boolean isError() {
    return error;
}

public String getMessage() {
    return message;
}

public getCervicalROM getUser() {
    return user;
}

} }


APIService APIService

  @FormUrlEncoded
 @POST("getCervicalROM")
 Call<PhpResponse> getCervicalROM (@Field("ID") String ID);

getCervicalROM is also name of index call. getCervicalROM也是索引调用的名称。 The PHP Slim Returns data in postman so this last stuff is most likely fine. PHP Slim返回邮递员中的数据,因此最后一件事可能很好。

 $app->post('/getCervicalROM', function(Request $request, Response $response) 
{

 if(!haveEmptyParameters(array('ID'), $response)){
    $request_data = $request->getParsedBody(); 
    $ID = $request_data['ID'];

 $db = new spinalromdbupload; 
 $result = $db->ptexists($ID);

if($result == DATA_FOUND){
 $user = $db->getCervicalROM($ID);

$response_data = array();

 $response_data['error'] = false; 
 $response_data['message'] = 'Data Aquired';
 $response_data['user'] = $user; 

 $response->write(json_encode($response_data));  
 return $response
->withHeader('Content-type', 'application/json')
->withStatus(200);  

 } else if ($result == DATA_NOT_FOUND){
 $response_data = array();
        $response_data['error']=true; 
        $response_data['message'] = 'No ROM data entered for this pt';
        $response->write(json_encode($response_data));
        return $response
            ->withHeader('Content-type', 'application/json')
            ->withStatus(422);   
 }}
  return $response
    ->withHeader('Content-type', 'application/json')
    ->withStatus(422);  
 });

the php file php文件

  public function getCervicalROM($ID){
        $stmt = $this->con->prepare("SELECT RACERVICALROT, LACERVICALROT FROM 
  spinalromdb WHERE ID = ?");
        $stmt->bind_param("s", $ID);
        $stmt->execute(); 
        $stmt->bind_result($RACERVICALROT, $LACERVICALROT);
        $stmt->fetch(); 
        $user = array(); 
        $user['RACERVICALROT'] = $RACERVICALROT; 
        $user['LACERVICALROT'] = $LACERVICALROT; 
        return $user; 
    }

     private function isIDExist($ID){
        $stmt = $this->con->prepare("SELECT ID FROM spinalromdb WHERE ID = 
 ?");
        $stmt->bind_param("s", $ID);
        $stmt->execute(); 
        $stmt->store_result(); 
        return $stmt->num_rows > 0;  
    }

You should provide default constructors for your model classes if you add constructurs with parameters. 如果添加带有参数的构造器,则应为模型类提供默认构造器。

In PhpResponse class, add: PhpResponse类中,添加:

public PhpResponse() {}

In getCervicalROM class, add: getCervicalROM类中,添加:

public getCervicalROM() {}

changed code status and code above works fine. 更改代码状态和​​上面的代码可以正常工作。 In the PHP index file it should be 201 not 200. 在PHP索引文件中,它应该是201而不是200。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM