Android MySQL连接教程

时间:2016-04-13 19:41:13

标签: java php android mysql

我使用了本教程(https://www.simplifiedcoding.net/android-mysql-tutorial-to-perform-basic-crud-operation/)并且一切正常,但是当我点击View Employee按钮时,它显示为空屏幕。有谁知道什么是错的?

我的 OneEvent.java 类(教程中的 ViewEmployee.java

public class OneEvent extends AppCompatActivity implements View.OnClickListener {

    private TextView tvId, tvTitle, tvDate, tvTime, tvAddress, tvPhone, tvDescription;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.one_event);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Intent intent = getIntent();
        id = intent.getStringExtra(Config.EVENT_ID);

        tvId = (TextView) findViewById(R.id.tvId);
        tvTitle = (TextView) findViewById(R.id.tvEventTitle);
        tvDate = (TextView) findViewById(R.id.tvEventDate);
        tvTime = (TextView) findViewById(R.id.tvEventTime);
        tvAddress = (TextView) findViewById(R.id.tvEventAddress);
        tvPhone = (TextView) findViewById(R.id.tvEventPhone);
        tvDescription = (TextView) findViewById(R.id.tvEventDescription);

        tvId.setText(id);
        getEvent();
    }

    private void getEvent(){
        class GetEvent extends AsyncTask<Void,Void,String>{
            ProgressDialog loading;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(OneEvent.this,"Loading...","Wait...",false,false);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                showEvent(s);
            }

            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                String s = rh.sendGetRequestParam(Config.URL_GET_EMP,id);
                return s;
            }
        }
        GetEvent ge = new GetEvent();
        ge.execute();
    }

    private void showEvent(String json){
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
            JSONObject c = result.getJSONObject(0);
            String title = c.getString(Config.TAG_TITLE);
            String date = c.getString(Config.TAG_DATE);
            String time = c.getString(Config.TAG_TIME);
            String address = c.getString(Config.TAG_ADDRESS);
            String phone = c.getString(Config.TAG_PHONE);
            String description = c.getString(Config.TAG_DESCRIPTION);

            tvTitle.setText(title);
            tvDate.setText(date);
            tvTime.setText(time);
            tvAddress.setText(address);
            tvPhone.setText(phone);
            tvDescription.setText(description);

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

    }

    @Override
    public void onClick(View v) {

    }
}

Config.java

public class Config {

    public static final String URL_ADD="http://diplomandroid.esy.es/create_event.php";
    public static final String URL_GET_ALL = "http://diplomandroid.esy.es/get_all_events.php";
    public static final String URL_GET_EMP = "http://diplomandroid.esy.es/get_event_details.php.?id=";

    public static final String EVENTS_COLUMN_ID = "_id";
    public static final String EVENTS_COLUMN_TITLE = "title";
    public static final String EVENTS_COLUMN_DATE = "date";
    public static final String EVENTS_COLUMN_TIME = "time";
    public static final String EVENTS_COLUMN_ADDRESS = "address";
    public static final String EVENTS_COLUMN_PHONE = "phone";
    public static final String EVENTS_COLUMN_DESCRIPTION = "description";

    public static final String TAG_JSON_ARRAY= "result";
    public static final String TAG_ID = "_id";
    public static final String TAG_TITLE = "title";
    public static final String TAG_DATE = "date";
    public static final String TAG_TIME = "time";
    public static final String TAG_ADDRESS = "address";
    public static final String TAG_PHONE = "phone";
    public static final String TAG_DESCRIPTION = "description";

    public static final String EVENT_ID = "event_id";
}

1 个答案:

答案 0 :(得分:0)

正如Juliano所述,当您从服务器请求时,您没有得到任何内容,因为您的网址不正确。

 public static final String URL_GET_EMP = "http://diplomandroid.esy.es/get_event_details.php.?id=";

应该是:

 public static final String URL_GET_EMP = "http://diplomandroid.esy.es/get_event_details.php?id=";