gpt4 book ai didi

java - 在获得 Volley 响应时始终获得 "please check the number"

转载 作者:行者123 更新时间:2023-12-01 10:36:31 25 4
gpt4 key购买 nike

当用户发布现有的ticknumber时,我有一个php文件,php将回显已发布的一个数组的JSON代码,我将其与Activity链接,该Activity将执行php文件的相同工作,但当我单击按钮时,我会收到toast消息“请检查数字”,例如 php 页面 echo“错误”,但这不是真的,因为我通过 postman 尝试过,它可以工作

<?php


if ($_SERVER ['REQUEST_METHOD']=='POST') {

$ticketnumber = $_POST['ticketnumber'];

require_once('config.php');

$con->set_charset('utf8');

$sql = " SELECT * FROM contact WHERE ticketnumber = '$ticketnumber' ";

$res = mysqli_query($con, $sql);

$result = array();

while($get = mysqli_fetch_array($res))
{
array_push($result,array('ticketnumber' =>$get[5], 'subject' =>$get[4],'response' =>$get[6]));
}
if(!empty($result)){
echo json_encode(array("responseticket"=>$result));
} else {
echo " error";
}

}
?>

supportActivity.java

public class supportActivity extends Fragment implements View.OnClickListener{

private EditText ticketsupport;
private Button button;
private List<supportContent> con = new ArrayList<supportContent>();
private ListView supportlist;
private supportAdapter adapter;
private String ticketinput;
private String url = "http://abvb.com/aaa/getticket.php";
private RequestQueue requestQueue1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_support, container, false);
getActivity().setTitle("Get Ticket");

ticketsupport = (EditText)view.findViewById(R.id.insertticketnumber);
supportlist = (ListView)view.findViewById(R.id.supportlistview);
adapter = new supportAdapter(getActivity(), con);
supportlist.setAdapter(adapter);
button = (Button)view.findViewById(R.id.buttonsupprt);

button.setOnClickListener(this);



return view;
}

private void inquiry() {

ticketinput = ticketsupport.getText().toString().trim();

StringRequest stringRequest1 = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {

@Override
public void onResponse(String response) {
if (response.trim().equals("error")) {

Toast.makeText(getActivity(), "please check the number", Toast.LENGTH_SHORT).show();
} else {


try {
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = jsonObject.getJSONArray("responseticket");

JSONObject jTicket = jsonArray.getJSONObject(0);
String Ticketnumber = jTicket.getString("ticketnumber");
String Subject = jTicket.getString("subject");
String Response = jTicket.getString("response");

supportContent support = new supportContent();

support.setTicketnumber(Ticketnumber);
support.setSubject(Subject);
support.setResponse(Response);

con.add(support);


} catch (JSONException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();

}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), "something wrong" , Toast.LENGTH_SHORT).show();
}
}) {

@Override
protected Map<String,String> getParams() throws AuthFailureError{
Map<String,String> map = new HashMap<String,String>();
map.put("ticknumber", ticketinput);
return map;
}
};

requestQueue1 = Volley.newRequestQueue(getActivity().getApplicationContext());
requestQueue1.add(stringRequest1);

}

@Override
public void onDestroy(){
super.onDestroy();
}


@Override
public void onClick(View view){
inquiry();
}
}

当ticketnumber存在时,php将像这样回显JSON

{"responseticket":[{"ticketnumber":"285","subject":"\u0627\u062d\u0628\u0643\u06‌​45 \u0643\u0644\u0643\u0645","response":"vcvc"}]} 

最佳答案

您的 PHP 需要 POST 参数中包含“ticketnumber”:

$ticketnumber = $_POST['ticketnumber'];

更改您的 Android 代码:

@Override
protected Map<String,String> getParams() throws AuthFailureError{
Map<String,String> map = new HashMap<String,String>();
map.put("ticknumber", ticketinput);
return map;
}

至:

 @Override
protected Map<String,String> getParams() throws AuthFailureError{
Map<String,String> map = new HashMap<String,String>();
map.put("ticketnumber", ticketinput);
return map;
}

关于java - 在获得 Volley 响应时始终获得 "please check the number",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34695177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com