0

I have a question to complete my android app. First, I'll quickly explain what I'm doing.

My application is like a voting system and it will has only 2 buttons (Yes / No). Initially created a logic to when clicked a button, fill a variable with yes or no.

But I'm trying to pass the direct value of the button, instead of filling out variables. At this time, when I click the yes button, the application is exploding when you try to parse.

Can someone help me? thank you very much. Below is the code (java and xml interface)

MainActivity.java

package com.clubee.vote;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int loader = R.drawable.loader;
    ImageView image = (ImageView) findViewById(R.id.imgVotacao);
    String image_url = "http://clubee.com.br/imgs/vote_imgs/Edicao.png";
    ImageLoader imgLoader = new ImageLoader(getApplicationContext());
    imgLoader.DisplayImage(image_url, loader, image);

    Button btnVotoSim = (Button) findViewById(R.id.btnSim);

    btnVotoSim.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            new InsereVoto().execute();
        }
    });
}

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

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


    protected String doInBackground(String... args) {
        String tipoVoto = "sim";

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tipoVoto", tipoVoto));

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

        Log.d("Create Response", json.toString());

        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                Intent i = new Intent(getApplicationContext(), Splashscreen.class);
                startActivity(i);

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

        return null;
    }

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();
EditText tipoVoto;
private static String url_Insere_voto = "http://dev.clubee.com.br/insereVoto.php";

private static final String TAG_SUCCESS = "success";

}

And the activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fffcfffa"
tools:context=".MainActivity">

<ImageView
    android:id="@+id/imgLogo"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    android:src="@drawable/bkg_app" />

<TextView
    android:id="@+id/infoVotacao"
    style="@style/CodeFont"
    android:layout_below="@id/imgLogo"
    android:text="@string/infoVotacao"/>

<ImageView
    android:id="@+id/imgVotacao"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_below="@+id/infoVotacao"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    android:gravity="bottom">

    <Button
        android:id="@+id/btnSim"
        android:layout_width="0.0dip"
        android:layout_height="wrap_content"
        android:text="SIM"
        android:textColor="#FFFFFF"
        android:layout_weight="1.0"
        android:background="@drawable/votebtngreen_btn_default_holo_light"/>

    <Button
        android:id="@+id/btnNao"
        android:layout_width="0.0dip"
        android:layout_height="wrap_content"
        android:text="NÃO"
        android:textColor="#FFFFFF"
        android:layout_weight="1.0"
        android:background="@drawable/votebtnred_btn_default_holo_light"/>

</LinearLayout>

LOGCAT:

02-14 13:16:36.995 9799-10217/com.clubee.vote E/JSON Parser﹕ Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject 
02-14 13:16:36.995 9799-10217/com.clubee.vote W/dalvikvm﹕ threadid=13: thread exiting with uncaught exception (group=0x415bdd88) 
02-14 13:16:37.005 9799-10217/com.clubee.vote E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1 Process: com.clubee.vote, PID: 9799 java.lang.RuntimeException: An error occured while executing doInBackground() 
5
  • Shows your logcat. What error appear? Commented Feb 14, 2015 at 15:07
  • Tks for response @tomloprod 02-14 13:16:36.995 9799-10217/com.clubee.vote E/JSON Parser﹕ Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject 02-14 13:16:36.995 9799-10217/com.clubee.vote W/dalvikvm﹕ threadid=13: thread exiting with uncaught exception (group=0x415bdd88) 02-14 13:16:37.005 9799-10217/com.clubee.vote E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1 Process: com.clubee.vote, PID: 9799 java.lang.RuntimeException: An error occured while executing doInBackground() Commented Feb 14, 2015 at 15:17
  • @tomloprod, i think that the issue is JSON Parser. JSONParser jsonParser = new JSONParser(); //EditText inputName; //EditText inputPrice; //EditText inputDesc; EditText tipoVoto; but do not know how to directly pass the value of the button. I think my button do not have a setted value as default Commented Feb 14, 2015 at 15:20
  • Maybe this link will be useful for you: stackoverflow.com/a/16663548/4359029 Commented Feb 14, 2015 at 15:42
  • It looks like you're trying to parse an XML string as if it were JSON. Commented Feb 14, 2015 at 23:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.