duanpacan9388 2016-06-24 12:06
浏览 41

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

I am a newer android developer , and i try to make an app which depend on MySQL. so i have MySQL database and I have A php that return its data in Json format at this link here. so i make a simple app that take this data and show it in list view by AsyncTask & Service Handler .

Note 1: I try this app with another database [Not Free Domain/website] and it work But with my database didn't work [free hosting]

Note 2: I try to comment the "Try & Catch" code at doInBackground method at AsyncTask class & make a dummy data manually So the app works !!, so what???!!

Update: i used the emulator and i got some red massages that i do not understand what its mean so i take it as screen shot massage 1 massage 2

My php code:

 <?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>

My java code

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 Code

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 code

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条回答 默认 最新

  • duanhoupeng6642 2016-06-25 10:48
    关注

    update: every thing was right, problem was in hosting server when i change hosting server , every thing work probably Thanks for interresting

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?