当StringBuilder为Null时,它不会继续

时间:2013-01-06 11:22:08

标签: php android mysql stringbuilder

我是初级Android开发人员,我使用JSON从Web服务器上的PHP文件获取数据,从MySql数据库获取数据

这是我的代码

public void onlinegetbooks() {

    try {

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("user",userid));

        HttpClient httpclient = new DefaultHttpClient(); 

        try{
            HttpPost httppost = new HttpPost("http://url/mybooks.php");

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        }catch(Exception e){
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
        }
        //buffered reader
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 80);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line = "0";
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            resultt = sb.toString();

            System.out.println("in onlinegetbooks, resultt: "+resultt); 
            System.out.println("onlinegetbooks StringBuilder: "+sb);

        }catch(Exception e){
            Toast.makeText(getApplicationContext(), "invalid", Toast.LENGTH_LONG).show();
        }

                            resultt = resultt.substring(1);
            jArray = new JSONArray(resultt);
        System.out.println("in onlinegetbooks jArray.length(): "+jArray.length());

        try{

            JSONObject json_data = null;

            for (int i = 0; i < jArray.length(); i++) {
                json_data = jArray.getJSONObject(i); 

                targetcover=json_data.getString("bo_cover_img");
                targetbname = json_data.getString("bo_name"); 
                targetbpath = json_data.getString("bo_path"); 
                targetbpartion=json_data.getString("bo_bg_id"); 
                targetauthname=json_data.getString("au_name");
                targetbokid=json_data.getString("bo_id");
                targetbokabout=json_data.getString("bo_about");

                coverr.add(targetcover); 
                bnamee.add(targetbname);
                bpathh.add(targetbpath );
                bpartionnn.add(targetbpartion);
                authnamee.add(targetauthname);
                bokiddd.add(targetbokid);
                bokabouttt.add(targetbokabout);

            }

            fflage=true;

    }catch(JSONException e){
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
        }   
    } catch (ParseException e) {
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
    }  
    catch (Exception e) {
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
    }       

}

我在下面的代码中调用了这个函数

private class LoadData extends AsyncTask<Void, Void, Void> { 
    private ProgressDialog progressDialog;  

    @Override

    protected void onPreExecute() {

        CharSequence contentTitle = getString(R.string.loginning);
        this.progressDialog = ProgressDialog.show(loginActivity.this,"",contentTitle); 

    }

    @Override
    protected void onPostExecute(final Void unused) {  
        this.progressDialog.dismiss();  

        if(fflage){

        Intent myBooks = new Intent(getApplicationContext(),TabsOFBooks.class);

        myBooks.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(myBooks);
        finish(); 

        } else {
            Toast.makeText(getApplicationContext(),R.string.noinfo, Toast.LENGTH_LONG).show();
        }

    }


    @Override
    protected Void doInBackground(Void... params) {

        try{
            bk.deletetable();
            recb.deletetable();
            mosb.deletetable();
            frboks.deletetable();
            ggp.deletetable();

        addusers();
        onlinegetbooks();
        addmybooks();

        onlinerecentbooks();

        fflage=true;
        }
        catch (Exception e) {
            System.out.println("No Information");

        }

        return null;

    }

}

我的问题出在resultt = sb.toString();

resulttNull时,不继续代码中的其他行而不打印此行

System.out.println("in onlinegetbooks jArray.length(): "+jArray.length());

1 个答案:

答案 0 :(得分:0)

目前您正在尝试显示来自doInBackground的Toast消息,这是不可能的,因为我们无法从AsyncTask的doInBackground方法访问UI元素。您可以更改AsyncTask类,以便在doInBackground执行完成时更新UI

private class LoadData extends AsyncTask<Void, Void, Void> { 
    private ProgressDialog progressDialog;  

    @Override

    protected void onPreExecute() {



    }




    @Override
    protected Void doInBackground(Void... params) {

        // call your methods here for getting data from server

        return null;

    }

     @Override
    protected void onPostExecute(final Void unused) {  
        this.progressDialog.dismiss();  
          // show toast messages or UI date UI here 

    }

}