我收到来自andriod部分的响应当它被发布为字符串请求bt如果我尝试在我的android部分发布json数组请求我无法在我的php链接中获取数据。我在http://require_once下面发布我的PHP代码(' Connection.php');
<?php require_once('Connection.php'); $json = array();
if($_SERVER['REQUEST_METHOD']=='POST'){ $username =
$_REQUEST['email']; $dummy = mysqli_query($link, "INSERT INTO email
(email) VALUES ('$username')"); $sql1 = "SELECT * FROM users1 WHERE
email='$username'"; if ($result1=mysqli_query($link, $sql1)){
$row1 = mysqli_fetch_array($result1); $user = $row1['uid'];
if($sql4 = mysqli_query($link,"SELECT * FROM wish_list WHERE user_id = '$user'")){ if (mysqli_num_rows($sql4) > 0){
while($row3 = mysqli_fetch_array($sql4)){
$item_id = $row3['item_id'];
if($sql6 = mysqli_query($link,"SELECT * FROM item_display WHERE item_display_id ='$item_id'")){
if (mysqli_num_rows($sql6) > 0){
$row6 = mysqli_fetch_array($sql6);
$item_name = $row6['item_name'];
$item_image = $row6['item_image'];
$item_price = $row6['item_price'];
$item_details = $row6['item_description'];
$arr = array($item_name,$item_image,$item_price,$item_details);
$array = array("Item" => $arr[0], "Image" => $arr[1], "Price" => $arr[2], "Description" => $arr[3]);
array_push($json,$array);
}
}
} }
$json1 = json_encode($json,JSON_UNESCAPED_SLASHES); echo $json1; } } }else{ $response["error"] = TRUE;
$response["response"] = "Not clicked order"; echo
json_encode($response); }
?>
我的Android代码是
public Fragment_Wishlist() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_wishlist, container, false);
final Context context = getActivity();
// RecyclerView recyclerView = (RecyclerView)
view.findViewById(R.id.recycler_view); //
recyclerView.setHasFixedSize(true); // // LinearLayoutManager
layoutManager = new LinearLayoutManager(context); //
recyclerView.setLayoutManager(layoutManager); // final
Wishlist_adapter wishlist_adapter= new Wishlist_adapter(context,
wDetailsList); // recyclerView.setAdapter(wishlist_adapter);
String email=AppController.getString(context,"email");
pDialog = new ProgressDialog(context);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
Map<String, String> postParam= new HashMap<String, String>();
postParam.put("email", email);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST,
AppConfig.URL_UserEmail_post, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, "hii"+response.toString());
hidePDialog();
try {
for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject = response.getJSONObject(i);
Details details = new Details();
details.setHome_item(jsonObject.getString("Item"));
details.setHome_itemPrice(jsonObject.getString("Price"));
details.setHome_itemImage(jsonObject.getString("image"));
details.setHome_itemCount(jsonObject.getString("count"));
// mDetailsList.add(details);
}
// homeAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
hidePDialog();
}
});
AppController.getInstance().addToRequestQueue(jsonArrayRequest,
tag_string_req);
return view;
}
答案 0 :(得分:0)
根据您的代码,请尝试执行以下某些版本
`StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_UserEmail_post, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "login Response: " + response.toString());
hidePDialog();
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("Product");
for(int i = 0; i < jsonArray.length(); i++){
JSONObject json = jsonArray.getJSONObject(i);
Details details =new Details();
details.setWishlist_item(json.getString("Item"));
details.setWishlist_price(json.getString("Price"));
details.setWishlist_image(json.getString("Image"));
String mi=details.getWishlist_item();
System.out.println("item"+mi);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() {
// Post params to login url
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "login");
String email=AppController.getString(context,"email");
params.put("email", email);
System.out.println("ema"+email);
return params;
}`