【发布时间】:2018-06-30 21:57:39
【问题描述】:
我的 android 应用需要发送一个字符串,并基于该字符串从数据库中获取响应。
我有一个 php,它从应用程序接收字符串,查询数据库,并使用 echo json_encode($response_array); 返回响应,它在浏览器上运行良好并以 json 对象格式回显。
但是,在应用程序中,我使用的是 Volley。上面的 php 数组 $response_array 发送多个字符串,我需要在应用程序文本视图中显示这些字符串。
我已经在 gradle 依赖项中设置了 volley。
但是,从我的手机(连接到我的笔记本电脑热点)上运行应用程序时,我得到的错误是“null”。这是应用程序的示例代码。
TextView o,t;
private RequestQueue requestQueue;
private static final String URL = "http://172.25.33.189/fadapp/mirror.php";
private StringRequest request;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
o=(TextView)findViewById(R.id.tryOne);
t=(TextView)findViewById(R.id.tryTwo);
requestQueue = Volley.newRequestQueue(this);
request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonobject = new JSONObject(response);
if (jsonobject.names().get(0).equals("name")) {
o.setText(jsonobject.getString("name"));
if(jsonobject.getString("stat").equals("1")) {
t.setText(R.string.inText);
t.setTextColor(Color.parseColor("#00FF00"));
} else {
t.setText(R.string.outText);
t.setTextColor(Color.parseColor("#FF0000"));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, ""+error.getMessage(), Toast.LENGTH_SHORT).show();;
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("speid", "database query string here");
return hashMap;
}
};
requestQueue.add(request);
【问题讨论】: