ERROR while trying to delete news (ANDROID JSON)

Asked

Viewed 135 times

0

At this link teaches to list comments (in my case news) using external database. There is the download of the project.

How can I delete a news by selecting and still make one appear Dialog asking whether or not to delete the news from the external database. I understand the following error in the log: FATAL EXCEPTION: AsyncTask #2

Follow the design I’m using.

package br.com.transparencia.conexaoweb;

import java.util.ArrayList;
import java.util.List;

import android.widget.ArrayAdapter;
import java.util.HashMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import br.com.transparencia.R;
import br.com.transparencia.conexaoweb.ConexaoHttpClient;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class ReadNoticiasPrefeitura extends ListActivity {
    private ProgressDialog pDialog;
    private Object json;
    private static final String READ_COMMENTS_URL = " "; // URL LISTAR NOTICIAS
    private static final String DELETE_COMMENTS_URL = " "; // URL DELETAR NOTICIAS

    private static final String TAG_SUCCESS = "success";
    private static final String TAG_TITULO = "titulo";
    private static final String TAG_POSTS = "posts";
    private static final String TAG_POST_ID = "id";
    private static final String TAG_NOTICIA = "noticia";

    // An array of all of our comments
    private JSONArray mComments = null;
    private ArrayList<HashMap<String, String>> mCommentList;
    public JSONParser jsonParser;

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

    @Override
    protected void onResume() {
        super.onResume();
        new LoadComments().execute();
    }

    public void sair(View v) {
        finish();
    }

    public void updateJSONdata() {

        mCommentList = new ArrayList<HashMap<String, String>>();

        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);
        try {
            mComments = json.getJSONArray(TAG_POSTS);
            for (int i = 0; i < mComments.length(); i++) {
                JSONObject c = mComments.getJSONObject(i);

                String titulo = c.getString(TAG_TITULO);
                String content = c.getString(TAG_NOTICIA);

                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_TITULO, titulo);
                map.put(TAG_NOTICIA, content);

                mCommentList.add(map);

            }

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

    private void updateList() {
        ListAdapter adapter = new SimpleAdapter(this, mCommentList,
                R.layout.single_post, new String[] { TAG_TITULO, TAG_NOTICIA
                         }, new int[] { R.id.titulo, R.id.noticia });

        setListAdapter(adapter);
        ListView lv = getListView();    

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

                new DeleteCard().execute();             

                  }         
        });
    }
    class DeleteCard extends AsyncTask<String, String, String> {

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

        /**
         * Deleting product
         * */
        protected String doInBackground(String... args) {
            int success;
            try {
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("titulo", TAG_TITULO));

                JSONObject json = jsonParser.makeHttpRequest(
                        DELETE_COMMENTS_URL, "POST", params);

                Log.d("DELETAR", json.toString());

                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    Intent i = getIntent();
                    setResult(100, i);
                    finish();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
        }
    }
/* ----------------------------------------------------------------------------------------*/

    public class LoadComments extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ReadNoticiasPrefeitura.this);
            pDialog.setMessage("Carregando...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected Boolean doInBackground(Void... arg0) {
            updateJSONdata();
            return null;

        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            pDialog.dismiss();
            updateList();
        }
    }
/* ########################################################################################################## */        
        public void mensagemExibir(String titulo, String texto)
        {
            AlertDialog.Builder mensagem = new AlertDialog.Builder(ReadNoticiasPrefeitura.this);
            mensagem.setTitle(titulo);
            mensagem.setMessage(texto);
            mensagem.setNeutralButton("OK", null);
            mensagem.show();
        }
}
  • Do you have a service on the server configured to receive this request? Check or delete news?

  • I have to list newsPrefeitura.php and delete exclusionNoticias.php

1 answer

1

Urls are not being filled in, with the service path:

private static final String READ_COMMENTS_URL = " "; // URL LISTAR NOTICIAS
private static final String DELETE_COMMENTS_URL = " "; // URL DELETAR NOTICIAS

I found nowhere setting these variables, see if that’s not it.

If it’s not that see if your service is working properly, a good tool for testing end-points is is.

Your error is generic, ta just stating that there was an error in AsyncTask #2. Try to get more details so we can help.

  • Aki the url are populated, so msm from the error and the app from force close(closes)

  • has how you download the guy’s project there in the link I mentioned? I used his project and tried to implement to delete and I can’t

  • You have implemented the web service in php?

  • yes, I only know the php language =( vc managed ae?

  • I can help with doubts, not do your job for you. And I’m not going to implement a web service just to test it. If you have tested your web service and it is working properly, I don’t know what might be going on.

  • this is not a job, I’m just studying android with this project, my area is php not java(android). Since you’re not curious to test and help someone, there’s nothing I can do.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.