DatePicker在Android中返回null

时间:2016-07-03 14:47:38

标签: php android sql-server-2008 datepicker

我有一个程序,可以在Android中使用PHP和DatePicker从数据库中选择数据。我的代码PHP工作,当我尝试在我的浏览器中运行,但当我想尝试在我的Android运行我的程序只是显示加载像这样http://prntscr.com/bo96a8。我不知道如何修复我的代码

我的代码Android就像这样

Toolbar toolbar;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
private EditText toDateEtxt;
private Button click;

private DatePickerDialog fromDatePickerDialog;
private DatePickerDialog toDatePickerDialog;

private SimpleDateFormat dateFormatter;
private ListView listView;

ArrayList<HashMap<String, String>> productsList;

// url to get all products list
private static String url_all_products = "http://192.168.1.113/history_ip.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "product";
private static final String TAG_NAME = "Name";
private static final String TAG_IP = "IPv4";
private static final String TAG_LOGIN = "Login Date";
private static final String TAG_LOGOUT = "Logout Date";

// products JSONArray
JSONArray products = null;
//search key value
public String searchkey;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.history_ip);
    // ListView listView = (ListView) findViewById(android.R.id.list);
    // Hashmap for ListView
    Intent myIntent = getIntent();
    // gets the arguments from previously created intent
    searchkey = myIntent.getStringExtra("key1");
    productsList = new ArrayList<HashMap<String, String>>();
    //ListView lv = getListView();
    // Loading products in Background Thread
    //new LoadAllProducts().execute();

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    //listView = (ListView) findViewById(R.id.lista);
    TypedValue typedValueColorPrimaryDark = new TypedValue();
    HistoryIP.this.getTheme().resolveAttribute(R.attr.colorPrimary, typedValueColorPrimaryDark, true);
    final int colorPrimaryDark = typedValueColorPrimaryDark.data;
    if (Build.VERSION.SDK_INT >= 21) {
        getWindow().setStatusBarColor(colorPrimaryDark);
    }
    dateFormatter = new SimpleDateFormat("yyyyMMdd", Locale.US);
    findViewsById();

    setDateTimeField();
    click = (Button) findViewById(R.id.btnbanned);
    click.setOnClickListener(this);
}
private void findViewsById() {
    toDateEtxt = (EditText) findViewById(R.id.date);
    toDateEtxt.setInputType(InputType.TYPE_NULL);
}

private void setDateTimeField() {
    toDateEtxt.setOnClickListener(this);
    Calendar newCalendar = Calendar.getInstance();
    toDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            toDateEtxt.setText(dateFormatter.format(newDate.getTime()));
            new LoadAllProducts().execute(dateFormatter.format(newDate.getTime()), searchkey);
        }
    },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}

// Get listview
protected ListView getListView(){
    if (listView == null) {
        listView = (ListView) findViewById(R.id.historyip);
    }
    return listView;
}

class LoadAllProducts extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(HistoryIP.this);
        pDialog.setMessage("Loading. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) {
        String date = args[0];
        String id = args[1];
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("date", date));
        params.add(new BasicNameValuePair("id", id));
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
        // Check your log cat for JSON reponse
        Log.d("All Products: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_PRODUCTS);

                // looping through All Products
                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable
                    String name = c.getString(TAG_NAME);
                    String ip = c.getString(TAG_IP);
                    String login = c.getString(TAG_LOGIN);
                    String logout = c.getString(TAG_LOGOUT);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_NAME, name);
                    map.put(TAG_IP, ip);
                    map.put(TAG_LOGIN, login);
                    map.put(TAG_LOGOUT, logout);

                    // adding HashList to ArrayList
                    productsList.add(map);
                }
            } else {
                //Toast.makeText(getApplicationContext(), "Doesn't have top cpt players now", Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        //runOnUiThread(new Runnable() {
        // public void run() {
        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                HistoryIP.this, productsList,
                R.layout.list_historyip, new String[]{
                TAG_NAME, TAG_IP, TAG_LOGIN},
                new int[]{R.id.title, R.id.rating, R.id.genre});
        // updating listview
        getListView().setAdapter(adapter);
        //}
        // });
    }
}
@Override
public void onClick(View view) {
    if(view == toDateEtxt) {
        toDatePickerDialog.show();
    } else if (view== click){
        new LoadAllProducts().execute();
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    getMenuInflater().inflate(R.menu.menu_news, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_refresh) {
        Intent i = new Intent(getApplicationContext(), HistoryIP.class);
        startActivity(i);
        //return true;
    }

    return super.onOptionsItemSelected(item);
}

任何人都可以帮助我吗?先谢谢

2 个答案:

答案 0 :(得分:0)

日期格式问题,您传递的日期格式不正确 请检查您的网络服务日期格式。通过时区传递或使用System.currentTimeMillis()http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html#currentTimeMillis()

 SimpleDateFormat dateFormatter =
        new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.US);

答案 1 :(得分:0)

你错过了打电话给节目。检查我的编辑。

public class ScoreActivity extends Activity {
    ListView listView;
     List<String> scoreList = new ArrayList<String>();





    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);



        listView=(ListView) findViewById(R.id.listView_score);




        SharedPreferences sharedPref = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        String name = sharedPref.getString("name","");

        SharedPreferences sharedPreferences = getSharedPreferences("userScore", Context.MODE_PRIVATE);
        String score = sharedPreferences.getString("score","");


        scoreList.add(name+"    "+score);







            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                    this,
                    R.layout.my_list, scoreList );

            listView.setAdapter(arrayAdapter);
        }


    }