【问题标题】:volley not populating ListView with remote server凌空没有用远程服务器填充 ListView
【发布时间】:2016-09-03 20:12:48
【问题描述】:

我的 android 应用程序连接到服务器并从数据库中获取信息并填写列表视图。它可以在我的 localhost 上与 xampp 一起正常工作。但是在将我的文件上传到我的服务器后它不起作用。日志中没有错误,除了。

D/Volley:[1] 2.onErrorResponse:ViewGuidesActivity

但是当我在我的网络浏览器上运行我的 PHP 文件时,它会完美地返回 JSON 数据。

public class ViewGuidesActivity extends AppCompatActivity {

private static final String TAG = ViewGuidesActivity.class.getSimpleName();
private ProgressDialog pDialog;
private List<Guide> guideList = new ArrayList<Guide>();
private ListView listView;
private CustomListAdapter adapter;


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

    listView = (ListView) findViewById(R.id.list);
    adapter = new CustomListAdapter(this, guideList);
    listView.setAdapter(adapter);

    pDialog = new ProgressDialog(this);
    // Showing progress dialog before making http request
    pDialog.setMessage("Loading...");
    pDialog.show();

    // Creating volley request obj
    JsonArrayRequest movieReq = new JsonArrayRequest(AppConfig.URL_GET_GUIDE_LIST,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            Guide guide = new Guide();
                            guide.setNgno(obj.getString("ngno"));
                            guide.setEmail(obj.getString("email"));
                            guide.setName(obj.getString("name"));


                            // adding movie to movies array
                            guideList.add(guide);

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

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            hidePDialog();

        }
    });

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(movieReq);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
            Guide newGuide = guideList.get(position);
            String email = newGuide.getEmail();
            String ngno = newGuide.getNgno();
            //System.out.println(email);

            Intent i = new Intent(getApplicationContext(), GuideDetails.class);
            i.putExtra("email", email);
            i.putExtra("ngno", ngno);
            startActivity(i);
        }
    });

}

我的 php 文件:

$con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "select * from guide"; 
$result = mysql_query($sql);
$json = array();

if(mysql_num_rows($result)){
         while($row=mysql_fetch_assoc($result)){
              $json[]=$row;
         }
}
mysql_close($con);
echo json_encode($json); 

我知道我正在使用一些不推荐使用的方法,因为我参考了一个过时的教程,并且我正在将所有 PHP 文件转换为使用 mysqli强>。但首先我需要让我的应用正常运行。

这是我的 JSON 数据:

http://srilankatouristguides.com/guideList.php

我在我的代码中使用了相同的 URL。可能是什么问题?无法弄清楚,因为它适用于我的 localhost。任何帮助,将不胜感激。

【问题讨论】:

  • 检查db用户名、密码和主机是否正确
  • 1.检查onResponse方法是否返回预期结果。 2)也许发布适配器代码。
  • 请告诉我们Log.d(TAG, response.toString()); 行正在打印什么 - 或者它被打印了。我们需要确认这是否是服务器没有返回数据或列表视图没有更新的问题。

标签: php android json listview android-volley


【解决方案1】:

我设法修复它。不使用 mysqli 会导致连接问题。可能服务器没有使用过时的方法,所以我的代码不起作用。将我的 PHP 代码转换为 mysqli 后,一切正常。谢谢各位大侠指点!

【讨论】:

    猜你喜欢
    • 2018-10-02
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 2013-08-12
    • 2015-11-14
    相关资源
    最近更新 更多