2016-06-24 14 views
1

私は新しいアンドロイドの開発者です。私はMySQLに依存するアプリを作ろうとしています。 ので、私は、MySQLデータベースを持っていると私は、このリンクhereJSON形式でそのデータを返すPHPを持っています。 私はこのデータを受け取り、リストビューでAsyncTask &サービスハンドラで表示する単純なアプリケーションを作成します。PHPコードが動作していてもMySQLからデータを読み込むことはできませんか?

注1:私は別のデータベース[未無料ドメイン/ Webサイト]で、このアプリを試してみて、それが仕事しかし、私のデータベースと動作しませんでした[無料ホスティング]

注2: I "試し&" AsyncTaskクラスのdoInBackgroundメソッドでコードをキャッチ & ダミーデータを手動で作成するアプリが動作するように!!

アップデート:私はエミュレータを使用し、私は私のPHPコード massage 1 massage 2

を撃った私はスクリーンとしてそれを取るように、何その平均理解していないいくつかの赤のマッサージだ:

<?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("[email protected]"); 
     Move1.setSAddress("CairoEgypt"); 

     MoveList.add(Move1); 

     MoveInfo Move2 = new MoveInfo(); 
     Move2.setId(2); 
     Move2.setSName("Ali"); 
     Move2.setSPass("456789"); 
     Move2.setSEmail("[email protected]"); 
     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; 
    } 
} 
+0

asynctaskのキャッチブロックで例外が発生しますか?もしそうなら、ここに投稿しなさい – SripadRaj

+0

私の電話はXiaomiであり、私はそれを私のPCにアドバイスとして接続するために問題があるので、私は私のアプリを作り、私はそれを試してみるたびに私の携帯電話にAPKを置く例外があるかどうかを知ることはできません –

+0

エミュレータで実行してみてください。 – SripadRaj

答えて

0

更新:すべてのものだったR私はサーバーのホスティングを変更したときに問題が発生しました。すべてのものがうまく動作します。interrestingのおかげで

関連する問題