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

时间:2018-08-07 20:22:49

标签: java android retrofit2

如何从json内的json返回String数据。 一种是检查是否有数据。然后201响应将导致数据变成字符串。

我尝试了以下操作,但不起作用。我怎么了有人可以向我解释,以便我学习吗?

//我按下按钮时调用的方法。
PhpResponse和getCervicalROM是下面列出的Model类。我尝试Toast以确保获取字符串变量数据。但是什么都没发生。


 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模型

 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;
}

}


语言反应模型

 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

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

getCervicalROM也是索引调用的名称。 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);  
 });

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;  
    }

2 个答案:

答案 0 :(得分:0)

如果添加带参数的构造器,则应为模型类提供默认构造器。

PhpResponse类中,添加:

public PhpResponse() {}

getCervicalROM类中,添加:

public getCervicalROM() {}

答案 1 :(得分:0)

更改代码状态和​​上面的代码可以正常工作。在PHP索引文件中,它应该是201而不是200。