Android asynctask不显示任何内容

时间:2013-12-27 16:54:53

标签: android mysql json android-asynctask

我有一个文件应该从我的MySQL数据库中提取产品列表,将它们编码为JSON数组,然后我需要让Android应用程序显示它们。

理想情况下,我希望它能够显示图标,但是我现在可以修改一下,我只是想让它工作

单击按钮时使用下面的当前代码,它不会执行任何操作。我的LogCat显示我的JSON非常好,但应用程序没有显示任何内容或崩溃。

public class GetProducts extends Activity {

//Delcare Variables


// JSON Response node names
private static String KEY_SUCCESS = "success";




/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.view_products);
    Functions userFunctions = new Functions();
    if(!userFunctions.isConnected(getApplicationContext())) 
    {
            displayDialogue("Connection Error","Your network settings are invalid - please turn on network settings ", "Open Network Settings");
    }
    if(!userFunctions.isUserLoggedIn(getApplicationContext())){
        Intent i = new Intent(GetProducts.this, FirstScreen.class);
        startActivity(i);
    }
    Button btnLogin;
    btnLogin = (Button)findViewById(R.id.btnGetProducts);


    //When our login button is pressed
    btnLogin.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            new LoginTask().execute();

        }

    });


    //Assign the variables our layout buttons


}

//We are using an AsyncTask so that this function runs on a seperate thread of the processor
//Since 4.2 + Android requires any web or HTTP activity to be ran seperate thread to the GUI
class LoginTask extends AsyncTask<String, String, String> {





            @Override
            protected String doInBackground(String... args) {
                // TODO Auto-generated method stub
                //Run the login function
                Login();
                return null;
            }






        }



public void displayDialogue(String title, String Message, String Button){
    final AlertDialog.Builder myDialogue = new AlertDialog.Builder(this);
    myDialogue.setTitle(title);
    myDialogue.setMessage(Message);
    TextView messageView = new TextView(this);
    messageView.setGravity(Gravity.CENTER);
    myDialogue.setView(messageView);

    myDialogue.setCancelable(false);

    myDialogue.setPositiveButton(Button,  new DialogInterface.OnClickListener()
    {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();

        }
    });

    AlertDialog dialog = myDialogue.create();
    dialog.show();
    }


public void Login(){




    try{
        Functions userFunctions = new Functions();
        final JSONObject json = userFunctions.getProducts();
        JSONObject json_user = json.getJSONObject("products");
        JSONArray jArray;
        jArray = json.getJSONArray("products");










        if (json.getString("success") != null) {

            String res = json.getString("success"); 
            if(Integer.parseInt(res) == 1){
                try{
                // user successfully logged in
                // Store user details in SQLite Database

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                                try {
                                    displayDialogue("Error",json.getString("name"), "Try Again");
                                } catch (JSONException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

                        }               
                    });




                // Clear all previous data in database




                // Close Login Screen
                finish();
                }
                catch(final Exception e){


                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            displayDialogue("Error",e.toString(), "Try Again");
                        }
                    });
                }


    }
            else{
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        displayDialogue("Error", "Error. \nWrong username / password combination please re-enter the details", "Try Again");
                    }
                });
            }
        }else{

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    displayDialogue("Error", "Error. \nGeneric Error", "Try Again");
                }
            });


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


}



}        

JSON结果: - (打印到Logcat)

12-27 16:53:45.011: E/JSON(23028): {"tag":"getProducts","success":1,"error":0,"products":{"name":"Coffee1","price":"4.00","image":"http:\/\/test.com"}}{"tag":"getProducts","success":1,"error":0,"products":{"name":"Coffee2","price":"5.00","image":"http:\/\/test2.com"}}

任何帮助都会很棒,谢谢!

0 个答案:

没有答案