下面这段代码显示了从我的服务器到我的android工具栏或操作栏的内容,但我想将代码转换为Activity
的正文,如用户全名,电子邮件用户名等。字符串{{1用于显示来自服务器的其他点我可以操作
URL = Config.Base_Url+"get/gtuspo.php";
PHP
txtName = (TextView) findViewById(R.id.name);
txtEmail = (TextView) findViewById(R.id.email);
txtUser = (TextView) findViewById(R.id.user);
// Showing Points on ToolBar Menu
@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
// executing retreival task here
String URL = Config.Base_Url+"get/gtuspo.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//System.out.println(response);
Toast.makeText(MainActivity.this,response,Toast.LENGTH_SHORT).show();
menu.findItem(R.id.points).setTitle("Points :" + response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("username",App.getInstance().getUsername());
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
return super.onPrepareOptionsMenu(menu);
}
String URL = Config.Base_Url+"get/gtuspo.php";
并在<?php
include '../connect_database.php';
if($_SERVER['REQUEST_METHOD']=='POST') {
$username = $_POST['username'];
$sql = "SELECT points FROM users WHERE login = '$username'";
$result = $connect->query($sql);
$row = $result->fetch_assoc();
echo $row["points"];
} else {
echo '404 - Not Found <br/>';
echo 'the Requested URL is not found on this server ';
}
?>
上显示姓名。
Actionbar
PHP
String `URL = Config.Base_Url+"get/name.php";
这是数据库结构
<?php
include '../connect_database.php';
if($_SERVER['REQUEST_METHOD']=='POST') {
$username = $_POST['username'];
$sql = "SELECT fullname FROM users WHERE login = '$username'";
$result = $connect->query($sql);
$row = $result->fetch_assoc();
echo $row["fullname"];
} else {
echo '404 - Not Found <br/>';
echo 'the Requested URL is not found on this server ';
}
?>
答案 0 :(得分:0)
将其另存为 user_info.php
onReponse
现在,在您的Android代码中,在您的@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse= new JSONObject(response);
txtName.setText(jsonResponse.getString("fullname"));
txtEmail.setText(jsonResponse.getString("email"));
txtPoints.setText(jsonResponse.getString("points"));// you need to create this textView txtPoints.
} catch (Throwable t) {
Log.e("onResponse", "The response: " + response + " seems to not be json formatted.");
}
}
中执行以下操作:
注意: Volley 提供了一种获取json响应的方法,但为了不改变您的代码,我们将这样做。
String URL = Config.Base_Url + "get/user_info.php";
不要忘记:
Pattern p = Pattern.compile("^(?:.*?\\sof\\s*)?(.*)");
Matcher m = p.matcher("94km SSE of Taron, Papua New Guinea");
if (m.find()) {
System.out.println(m.group(1));
}