基本上这里我使用url连接连接到php,一切都是工作查找登录和注册阶段,现在我想获取我在php文件中创建的json对象,然后将其显示在字符串视图中,所以任何人对此有所了解?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
dataField = (TextView) findViewById(R.id.data);
btnDis = (Button) findViewById(R.id.btnClick);
btnDis.setOnClickListener (new View.OnClickListener(){
@Override
public void onClick(View v){
strUrl ="http://10.0.2.2/android/display.php";
new jsonParse().execute();
}
});
}
public class jsonParse extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
Toast.makeText(EditActivity.this,""+result,Toast.LENGTH_LONG ).show();
}
@Override
protected String doInBackground(String... params) {
try{
URL url = new URL (strUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.connect();
//get response from server
BufferedReader bf = new BufferedReader(new InputStreamReader(con.getInputStream()));
String value = bf.readLine();
System.out.println("result is"+value);
result = value;
String finalJson = bf.toString();
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = parentObject.getJSONArray("data");
JSONObject finalObject= parentArray.getJSONObject(0);
String username = finalObject.getString("NAME");
String age = finalObject.getString("AGE");
String result = username +" - "+age;
return result;
}
catch(Exception e){
System.out.println(e);
}
return null;
}
}
}
这是我的php文件,名为display.php
<?php
require "conn.php";
require_once "global.php";
//get record from databases
$query = "SELECT * FROM user";
if($result = mysqli_query($conn, $query)){
// printf("%d", mysqli_num_rows($result));
if(mysqli_num_rows($result) > 0){
$status = 'true';
$message = 'data retrieved successfully';
while($row = mysqli_fetch_assoc($result)){
$name = $row['name'];
$age = $row['age'];
$username = $row['username'];
// echo $name."\n\n";
// echo $age."\n\n";
// echo $username."\n\n";
$data .= '{"NAME" : "'.$name.'", "AGE" : "'.$age.'", "USERNAME" : "'.$username.'"},';
}
}else{
$status = 'false';
$message = 'data retrieved failed';
$data = '';
}
mysqli_free_result($result);
mysqli_close($conn);
}else{
$message = mysqli_error($conn);
}
$output = '{"status": "'.$status.'","message":"'.$message.'", "data": ['.rtrim($data, ',').']}';
echo $output;
?>
答案 0 :(得分:0)
您可以使用Java库GSON https://github.com/google/gson。
要将json格式转换为java String.class,请写:
String str = gson.fromJson("\"abc\"", String.class);
答案 1 :(得分:0)
回调进入onPostExecute后,String参数可能在键值对中。所以要将String转换为JsonObject使用 -
@Override
protected void onPostExecute(String s) {
try {
JSONObject jsonObject = new JSONObject(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
答案 2 :(得分:0)
您可以使用
dataField.setText(结果);
首先创建一个像这样的POJO类
class BaseClass {
Boolean status;
String message;
String data;
RowClass row;
public Boolean getStatus() {
return status;
}
public String getMessage() {
return message;
}
public String getData() {
return data;
}
public RowClass getRow() {
return row;
}
class RowClass {
String name;
String username;
int age;
String data;
public String getName() {
return name;
}
public String getUsername() {
return username;
}
public int getAge() {
return age;
}
public String getData() {
return data;
}
}
}
然后通过
检索数据BaseClass jsonObject = new BaseClass();
jsonObject = gson.fromJson(finalJson, BaseClass.class);
答案 3 :(得分:0)
private static String jsonToString(final Object obj) {
String result;
try {
final ObjectMapper mapper = new ObjectMapper();
final String jsonContent = mapper.writeValueAsString(obj);
result = jsonContent;
} catch (JsonProcessingException e) {
result = "JSON Parsing error";
}
return result;
}