我正在为一个项目开发一个概念证明,我将一些数据从android发送到mysql数据库,并从mysql数据库中获取一些数据到android。我通过PHP脚本成功完成了这项工作,当我使用本地主机ip时它正在工作但是当我在Web服务器上托管它时它给了我无效的json。我认为这是AES安全问题。我正在使用retrofit2来提出请求。以下是我提出改装请求的代码。
//Creating a retrofit object
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(RetrofitApi.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
//creating the api interface
RetrofitApi api = retrofit.create(RetrofitApi.class);
//now making the call object
//Here we are using the api method that we created inside the api interface
Call<ClientList> call = api.getClient();
//then finallly we are making the call using enqueue()
//it takes callback interface as an argument
//and callback is having two methods onRespnose() and onFailure
//if the request is successfull we will get the correct response and onResponse will be executed
//if there is some error we will get inside the onFailure() method
call.enqueue(new Callback<ClientList>() {
@Override
public void onResponse(Call<ClientList> call, Response<ClientList> response) {
//In this point we got our client list
//thats damn easy right ;)
final ClientList clientList = response.body();
Log.v("Retrofit Response", response.body().toString());
Log.v("Test", "" + clientList.getClientList().size());
//now we can do whatever we want with this list
ClientRecyclerAdapter adapter = new ClientRecyclerAdapter(clientList.getClientList(), MainActivity.this);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Client client = clientList.getClientList().get(position);
Intent intent = new Intent(MainActivity.this, SiteActivity.class);
intent.putExtra("client_id", client.getId());
startActivity(intent);
}
});
}
@Override
public void onFailure(Call<ClientList> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
这是处理json
编码的脚本 <?php
/*
* Following code will get all the
clients in the database
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
//this function help us to get all client
//clients table
function getAllClient($db){
$response["clients"] = array();
$stmt = $db->query("SELECT * FROM clients");
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(($row_count = $stmt->rowCount()) > 0){
$response["clients"] = $rows;
echo json_encode($response);
}else{
// no client found
$response["success"] = 0;
$response["message"] = "No client found in the database";
// echo no client JSON
echo json_encode($response);
}
//$row_count = $stmt->rowCount();
}
getAllClient($connection);
?>
当我在Web浏览器上测试API时,它返回了一个json,它在JSONFormatter上传递了绿色测试。如何处理AES安全性以获得retrofit2(Android)可以处理的格式正确的JSON。