1

私はジュニアAndroid開発者であり、MySqlデータベースからデータを取得するWebサーバー上のPHPファイルからデータを取得するためにJSONを使用しています

これは私のコードです

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();

resulttがである場合Null、コード内の他の行を続行せず、この行を出力しません

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

1 に答える 1

0

現在、doInBackgroundからToastメッセージを表示しようとしていますが、AsyncTaskのdoInBackgroundメソッドからUI要素にアクセスできないため不可能です。doInBackgroundの実行が完了したときにUIを更新する場合は、AsyncTaskクラスを変更できます。

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 

    }

}
于 2013-01-06T12:53:09.383 に答える