即使php代码正常工作,也无法从MySQL加载我的数据?

时间:2016-06-24 12:06:39

标签: java php android mysql android-asynctask

我是一个较新的Android开发人员,我尝试制作一个依赖MySQL的应用程序。 所以我有MySQL数据库,我有一个 php ,可以通过此链接 here Json 格式返回其数据。 所以我制作了一个简单的应用程序来获取这些数据并通过AsyncTask& amp;服务处理程序。

  

注意1:我尝试使用其他数据库[非自由域名/网站]这个应用程序但它可以工作但是我的数据库没有工作[免费托管]

     

注2:我尝试评论"尝试&捉" AsyncTask类的doInBackground方法的代码&手动创建虚拟数据所以应用程序正常工作!!,那么??? !!

更新:我使用了模拟器,我得到了一些红色按摩,我不明白它是什么意思所以我把它作为屏幕截图 massage 1 massage 2

我的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;
   }
}

1 个答案:

答案 0 :(得分:0)

更新:每件事情都是对的,当我更改托管服务器时,问题出现在托管服务器上,每件事情都可能感谢interresting