2017-07-30 117 views
-3

MainActivity:凌空onResponse的Android不行

RequestQueue requestQueue; 
String url = "http://andriodtest.eb2a.com/show.php"; 
TextView textView; 
ListView listView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    textView = (TextView) findViewById(R.id.TextView); 

requestQueue = Volley.newRequestQueue(this); 
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, 
     new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_LONG).show(); 

       try { 
        JSONArray jsonArray = response.getJSONArray("users"); 
        for (int i = 0; i < jsonArray.length(); i++) { 
         JSONObject respons = jsonArray.getJSONObject(i); 
         String id = respons.getString("id"); 
         String info = respons.getString("name"); 
         textView.append(id); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } 

     }, new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 
     Log.e("VOLLEY", "ERROR"); 
    } 
} 

); 
requestQueue.add(jsonObjectRequest); 

当运行应用程序没有数据称为 和文件配置为工作100%。

example

如果设置的TextView = “文本” 不起作用。

我认为问题是public void onResponse。 请帮我重要

+1

您的PHP后端未提供JSON,而是原始HTML。 – Enzokie

回答

0

你的代码看起来很好,但它看起来像你的服务器的响应没有返回一个JSON对象,而是设置一个cookie JavaScript文件(你可以试着打你的网址与邮差看的javascript回应我提到)。

Volley希望收到json响应,这可能是您的应用程序目前无法按预期工作的原因。

我可能会尝试改变服务器响应的方式。以下链接为解决此问题提供了一些建议: Why I can't retrieve data from my webserver as json but i can when i test it on localhost