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()
logcat. What error appear?