在我的项目中,我从JSONobject请求传递params中的用户的名字。然后它会得到响应并填充文本视图。但是,我无法弄清楚为什么我的代码不起作用。我检查了我的PHP,当我在其中放入一个预定义的名字时它工作正常,所以我排除了一个Web服务问题。它首先获得响应然后通过参数吗?请帮忙
public class ProfileActivity extends AppCompatActivity {
TextView Username, Firstname, Lastname, Birthdate, Barangay;
String firstname;
String json_url = "http://localhost/android/getprofileinfo.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Username = (TextView)findViewById(R.id.usernameprofile);
Firstname = (TextView)findViewById(R.id.firstnameprofile);
Lastname = (TextView)findViewById(R.id.lastnameprofile);
Birthdate = (TextView)findViewById(R.id.birthdayprofile);
Barangay = (TextView)findViewById(R.id.barangayprofile);
final Bundle bundle = getIntent().getExtras();
firstname = bundle.getString(firstname);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, json_url, (String) null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Username.setText(response.getString("username"));
Firstname.setText(response.getString("firstname"));
Lastname.setText(response.getString("lastname"));
Birthdate.setText(response.getString("birthdate"));
Barangay.setText(response.getString("barangay"));
}
catch(JSONException e){
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ProfileActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
error.printStackTrace();
} //end of method onErrorResponse
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("firstname", firstname);
return params;
}
};
MySingleton.getmInstance(ProfileActivity.this).addTorequestque(jsonObjectRequest);
}
}
这是getpropileinfo.php
<?php
$firstname =$_POST["firstname"];
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','mydb');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
$sql = "SELECT username,firstname,lastname,birthdate,barangay FROM users
WHERE firstname LIKE '".$firstname."'; ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0)
{
$row = mysqli_fetch_assoc($result);
echo json_encode(array("username"=>$row['username'],
"firstname"=>$row['firstname'],
"lastname"=>$row['lastname'], "birthdate"=>$row['birthdate'],
"barangay"=>$row['barangay']));
}
?>
答案 0 :(得分:1)
您未正确使用<?php
$gl_TBLNAME = "";
$gl_QUERYSTRING = "";
function getCache($useCache, $serverQUERYSTRING, $serverPOST, $serverFILENAME) {
$ret = "";
if ($useCache === true) {
global $LGCACHEPDO;
global $gl_TBLNAME;
global $gl_QUERYSTRING;
$gl_QUERYSTRING = $serverQUERYSTRING;
if ($gl_QUERYSTRING == "") {
foreach ($serverPOST as $key => $value) $gl_QUERYSTRING = $gl_QUERYSTRING . $key . "=". urlencode($value) . "&";
$gl_QUERYSTRING = rtrim($gl_QUERYSTRING, "&");
}
$gl_TBLNAME = $serverFILENAME;
$getcache_PRST = $LGCACHEPDO->prepare("SELECT output FROM " . $gl_TBLNAME . " WHERE querystring = :querystring");
$getcache_PRST->bindValue(":querystring", $gl_QUERYSTRING);
$getcache_PRST->execute() or die($LGCACHEPDO->errorInfo());
if ($getcache_PRST->rowCount() == 1) {
$getcache_RSLT = $getcache_PRST->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT);
$ret = $getcache_RSLT["output"];
}
}
return $ret;
}
// more functions here
?>
,请使用您的通话活动getExtras()
提供您传递的密钥名称,例如
在调用活动传递意图中,如
putExtra()
然后在ProfileActivity中,
Intent i = new Intent(FirstActivity.this, ProfileActivity.class);
String strName = "some_name";
i.putExtra("key_username", strName);
startActivity(i);
转换为final Bundle bundle = getIntent().getExtras();
firstname = bundle.getString("key_username");
JsonArrayRequest
答案 1 :(得分:0)
可能是您的本地主机问题,Go through Genymotion正在使用您的PC IP地址。获取您的IP地址:
开始 - &gt; cmd - &gt; IPCONFIG 然后搜索IPv4,复制IP并将其粘贴到您的URL中。它应如下所示:
String YourURL =“http://192.168.0.106:8888/android/getprofileinfo.php”;
希望这也适合你。
答案 2 :(得分:0)
在方法参数中,删除json对象的字符串强制转换。
null
代替(String) null
。
答案 3 :(得分:0)
将您的JSONRequest更改为StringRequest。