即使 php 代码正在运行,也无法从 MySQL 加载我的数据?

Posted

技术标签:

【中文标题】即使 php 代码正在运行,也无法从 MySQL 加载我的数据?【英文标题】:Can't load my data from MySQL even though php code is working? 【发布时间】:2016-10-27 01:18:26 【问题描述】:

我是一个较新的 android 开发人员,我尝试制作一个依赖于 mysql 的应用程序。 所以我有 MySQL 数据库,并且我有一个 php,它在此链接 here 上以 Json 格式返回其数据。 所以我制作了一个简单的应用程序来获取这些数据并通过 AsyncTask & Service Handler 在列表视图中显示它。

注意 1:我在另一个数据库 [非免费域/网站] 上尝试使用此应用程序,它可以工作但我的数据库无法工作 [免费托管]

注意 2: 我尝试在 AsyncTask 类的 doInBackground 方法中注释 “Try & Catch”代码 并手动制作 虚拟数据所以该应用程序有效!!,那又怎样???!!

更新:我使用了模拟器,我得到了一些红色的按摩,我不明白它的意思,所以我把它当作屏幕截图

我的 php 代码:

 <?php
 $dbname = 'zoubg_18363398_center';
 $dbserver = 'sql104.kariya-host.com';
 $dbusername = 'zoubg_18363398';
 $dbpassword = '28721235';
 $dbconnect = new mysqli($dbserver, $dbusername, $dbpassword, $dbname);
 $getpostssql = "SELECT * FROM users";
 $posts = $dbconnect->query($getpostssql);
 $postsarray = array();
 while($row = mysqli_fetch_array($posts, MYSQL_ASSOC))
 $temp['id'] = $row['id'];
 $temp['name'] = $row['name'];
 $temp['password'] = $row['password'];
 $temp['email'] = $row['email'];
 $temp['adress'] = $row['adress'];
 array_push($postsarray, $temp);
 
 echo json_encode(array("posts"=>$postsarray), JSON_UNESCAPED_UNICODE);
</blink>

我的 java 代码

public class MoveActivity extends AppCompatActivity 

    ListView LVMove;
    MoveAdapter moveAdapter;
    ArrayList<MoveInfo> MoveList = new ArrayList<>();
    ProgressDialog pDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_move);
        LVMove = (ListView) findViewById(R.id.lv_test);
        
       // dummy data manually
       /*
        MoveInfo Move1 = new MoveInfo();
        Move1.setId(1);
        Move1.setSName("Ahmed");
        Move1.setSPass("123456");
        Move1.setSEmail("Ahmed@asdf.com");
        Move1.setSAddress("CairoEgypt");

        MoveList.add(Move1);

        MoveInfo Move2 = new MoveInfo();
        Move2.setId(2);
        Move2.setSName("Ali");
        Move2.setSPass("456789");
        Move2.setSEmail("Ali@asdf.com");
        Move2.setSAddress("AlexEgypt");
        */
        new GetMoves().execute("http://centertest.kariya-host.com/testjjjsn.php");
    

    class GetMoves extends AsyncTask<String, Void, Void> 

        @Override
        protected void onPreExecute() 
            super.onPreExecute();

            pDialog = new ProgressDialog(MoveActivity.this);
            pDialog.setMessage(" Please wait ... ");
            pDialog.setCancelable(false);
            pDialog.show();
        

        @Override
        protected Void doInBackground(String... strings) 

            String url = strings[0];

            ServiceHandler serviceHandler = new ServiceHandler();
            JSONObject jsonObject = serviceHandler.makeServiceCall(url, ServiceHandler.GET);

            try 
                JSONArray DATA = jsonObject.getJSONArray("posts");
                for (int i = 0; i < DATA.length(); i++) 

                    JSONObject item = DATA.getJSONObject(i);
                    MoveInfo Move = new MoveInfo();

                    int id = item.getInt("id");
                    String name = item.getString("name");
                    String password = item.getString("password");
                    String email = item.getString("email");
                    String adress = item.getString("adress");

                    Move.setId(id);
                    Move.setSName(name);
                    Move.setSPass(password);
                    Move.setSEmail(email);
                    Move.setSAddress(adress);
                    MoveList.add(Move);
                
             catch (JSONException e) 
                e.printStackTrace();
            
            return null;
        

        @Override
        protected void onPostExecute(Void aVoid) 
            super.onPostExecute(aVoid);
            if(pDialog.isShowing())
                pDialog.dismiss();
                moveAdapter = new MoveAdapter(MoveList, getApplicationContext());
                LVMove.setAdapter(moveAdapter);
            
        
    

ServiceHandler 代码

public class ServiceHandler 
    static String response = null;
    public final static int GET = 1;
    public final static int POST = 2;

    public ServiceHandler() 
    

    public JSONObject makeServiceCall(String url, int method) 
        return this.makeServiceCall(url, method, null);
    

    public JSONObject makeServiceCall(String url, int method,
                                  List<NameValuePair> params) 

        JSONObject jsonObject=null;
        try 
            // http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            // Checking http request method type
            if (method == POST) 
                HttpPost httpPost = new HttpPost(url);
                // adding post params
                if (params != null) 
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                
                httpResponse = httpClient.execute(httpPost);
             else if (method == GET) 
                // appending params to url
                if (params != null) 
                    String paramString = URLEncodedUtils
                            .format(params, "utf-8");
                    url += "?" + paramString;
                
                HttpGet httpGet = new HttpGet(url);
                httpResponse = httpClient.execute(httpGet);
            
            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);
            jsonObject=new JSONObject(response);

         catch (UnsupportedEncodingException e) 
            e.printStackTrace();
         catch (ClientProtocolException e) 
            e.printStackTrace();
         catch (IOException e) 
            e.printStackTrace();
         catch (JSONException e) 
            e.printStackTrace();
        
        return jsonObject;
    

MoveAdapter 代码

public class MoveAdapter extends BaseAdapter 
   ArrayList<MoveInfo> MoveList;
   Context context;
   LayoutInflater inflater ;

   public MoveAdapter (ArrayList<MoveInfo> MoveList, Context context)
       this.MoveList = MoveList;
       this.context = context;
       inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   

   @Override
   public int getCount() 
       return MoveList.size();
   

   @Override
   public Object getItem(int i) 
       return MoveList.get(i);
   

   @Override
   public long getItemId(int i) 
       return MoveList.get(i).getId();
   

   @Override
   public View getView(int i, View view, ViewGroup viewGroup) 
       if (view == null)
           view = inflater.inflate(R.layout.item_list, null);
       

       TextView TvIds = (TextView) view.findViewById(R.id.tv_Ids);
       TextView TvNames = (TextView) view.findViewById(R.id.tv_Names);
       TextView TvPasss = (TextView) view.findViewById(R.id.tv_Passs);
       TextView TvEmails = (TextView) view.findViewById(R.id.tv_emails);
       TextView TvAddresss = (TextView) view.findViewById(R.id.tv_addresss);

       TvIds.setText(MoveList.get(i).getId()+"");
       TvNames.setText(MoveList.get(i).getSName());
       TvPasss.setText(MoveList.get(i).getSPass());
       TvEmails.setText(MoveList.get(i).getSEmail());
       TvAddresss.setText(MoveList.get(i).getSAddress());

       return view;
   

【问题讨论】:

您的异步任务的 catch 块中是否有任何异常?如果是,请在此处发布 我的手机是小米,我无法将它连接到我的电脑,所以我构建了我的应用程序并将 apk 放到我的手机上,每次我不想尝试它,所以不知道有没有异常 在模拟器上运行并尝试。 我使用了模拟器,我得到了一些红色的按摩,我不明白它的意思,所以我把它当作屏幕截图并添加它作为更新,请看一下:) @SripadRaj 您在 MoveActivity 的异步任务的第 69 行的 doInBackground() 方法中遇到空指针异常。您能告诉我第 69 行是什么吗? 【参考方案1】:

更新:一切都是正确的,当我更改托管服务器时,问题出在托管服务器上,每件事都可能正常工作感谢您的关注

【讨论】:

以上是关于即使 php 代码正在运行,也无法从 MySQL 加载我的数据?的主要内容,如果未能解决你的问题,请参考以下文章

即使我们正在清理输入 mysql_real_escape_string 的 SQL 注入漏洞代码

即使由于错误导致插入失败,MySQL 自动增量值也会增加

即使使用示例代码,mmenu 也无法正常工作

PhpStorm 无法识别 Php 文件

即使mysqld在端口3306上运行,也无法连接到'localhost:3306'上的MySQL服务器

即使已经安装了可再发行组件,也无法运行 VS 2013 项目?