Android - Value null of type org.json.JSONObject$1 cannot be converted to JSONObject Android

 
Vista:
Imágen de perfil de Edorta

Value null of type org.json.JSONObject$1 cannot be converted to JSONObject Android

Publicado por Edorta (2 intervenciones) el 12/06/2015 12:26:35
Hola a todos, estoy recibiendo el siguiente error:


E/UsuariosGest﹕ doIn: Value null of type org.json.JSONObject$1 cannot be converted to JSONObject

Este es mi codigo java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
public class RatingActivity extends Activity {
 
private TextView tvUsu;
private final String TAG="CHAT";
private int mOP;    //1->Validar 2->Registrar
private ProgressDialog mPD;
private ImageView ivVotar;
private String votos;
private String ide;
private String usuario;
private TextView t1;
 
 
 
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rating);
 
    tvUsu = (TextView)findViewById(R.id.etUsu);
    ivVotar = (ImageView)findViewById(R.id.ivBotonVotar);
 
 
    Bundle extras = getIntent().getExtras();
    usuario = extras.getString("usuario");
    ide = extras.getString("ide");
    tvUsu.setText(usuario);
 
 
 
    RatingBar rr=(RatingBar) findViewById(R.id.ratingBar1);
       t1=(TextView) findViewById(R.id.textView1);
 
    rr.setRating((float) 0);
    rr.setStepSize((float) 1);
 
    rr.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
 
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating,
                                    boolean fromUser) {
            // TODO Auto-generated method stub
 
            t1.setText(String.valueOf(rating));
            votos = t1.getText().toString();
        }
    });
 
    ivVotar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
 
            mOP = 2;
 
            Bundle extras = getIntent().getExtras();
            String usuario = extras.getString("usuario");
            String ide = extras.getString("ide");
            votos = t1.getText().toString();
 
            if ((ide.length() > 0) && (t1.length() > 0 && usuario.length() > 0))  {
 
                //Lanzamos el SW:
                mPD = new ProgressDialog(RatingActivity.this);
                mPD.setCancelable(true);
                mPD.show();
                UsuariosGest usuariosGest = new UsuariosGest();
                usuariosGest.execute();
            }
 
 
 
        }
    });
}
 
public void btnValidar_Click(View view){
    mOP=1;
    String ide = "kaka";
    String l ="kuku";
 
    if ((ide.length()>0) && (l.length()>0)) {
 
        //Lanzamos el SW:
        mPD = new ProgressDialog(this);
        mPD.setCancelable(true);
        mPD.show();
        try {
            UsuariosGest usuariosGest = new UsuariosGest();
            usuariosGest.execute().get(5000, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            Log.e(TAG,"btnValidar_Click:"+e.getMessage());
            Toast.makeText(RatingActivity.this, "Sin conexión con el servidor", Toast.LENGTH_SHORT).show();
            if (mPD.isShowing()) mPD.dismiss();
        }
    }
}
 
public void btnInicio_Click(View view){
    Bundle extras = getIntent().getExtras();
    String usuario = extras.getString("usuario");
 
    Intent i = new Intent(RatingActivity.this, Lista_Activity.class);
    i.putExtra("usuario", usuario);
    startActivityForResult(i, 0);
    finish();
 
}
 
 
private class UsuariosGest extends AsyncTask<String, Void, JSONObject> {
    @Override
    protected JSONObject doInBackground(String... params) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://www.enarrations.com/votos_gest.php");
        JSONObject jsonResponse = null;
        try {   //Construimos el objeto cliente en formato JSON
            JSONObject jsonProd = new JSONObject();
            jsonProd.put("op", mOP);
            jsonProd.put("ide", ide);
            jsonProd.put("votos", votos);
            jsonProd.put("usuario", usuario);
            JSONArray postjson=new JSONArray();
            postjson.put(jsonProd);
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            httpPost.setHeader("json",jsonProd.toString());
            httpPost.getParams().setParameter("jsonpost",postjson);
            HttpResponse httpResp = httpClient.execute(httpPost);   //Realizamos la petición al servidor:
            if (httpResp.getEntity()!=null) {
                String respuesta = EntityUtils.toString(httpResp.getEntity());
                jsonResponse=new JSONObject(respuesta);
            }
        } catch (Exception e) {
            Log.e("UsuariosGest", "doIn: " + e.getMessage());
        }
        return jsonResponse;
    }
    @Override
    protected void onPostExecute(JSONObject jsonRespuesta) {
 
        Bundle extras = getIntent().getExtras();
        String usuario = extras.getString("usuario");
 
        try{
            if (jsonRespuesta!=null)  { //Obtenemos el parametro res: 1->OK, -1->Error
                int res = jsonRespuesta.getInt("res");
                String desc = jsonRespuesta.getString("desc");
                if (res>0) {    //Ha ido bien
                    Toast.makeText(RatingActivity.this, "Voto emitido!", Toast.LENGTH_SHORT).show();
                    Intent i = new Intent(RatingActivity.this, RankingActivity.class);
                    i.putExtra("usuario",usuario);
                    startActivityForResult(i,0);
                    finish();
                } else
                    Toast.makeText(RatingActivity.this, desc, Toast.LENGTH_SHORT).show();
            }
            else
                Toast.makeText(RatingActivity.this, "Error respuesta JSON null", Toast.LENGTH_SHORT).show();
        }catch (Exception e) {
            Log.e("UsuariosGest","onPost: "+e.getMessage());
        }
        mPD.dismiss();
        super.onPostExecute(jsonRespuesta);
    }
}
 
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
}

Este es mi archivo PHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
header("Content-Type: text/html;charset=utf-8");
 
class CVotos {
private $cod;
private $email;
private $votos;
public function getCod(){
    return $this->cod;
}
public function getEmail(){
    return $this->email;
}
public function getVotos(){
    return $this->votos;
}
public function __construct($cod,$email,$votos){
    $this->cod = $cod;
    $this->email = $email;
    $this->votos = $votos;
}
}
 
try {
if (isset($_SERVER['HTTP_JSON'])) {
    $json = utf8_encode($_SERVER['HTTP_JSON']);
    $datos = json_decode($json,true);
    $json_error= json_last_error();
    $email = $datos["usuario"];
            $cod =   $datos["ide"];
    $votos = $datos["votos"];
    $op = $datos["op"]; //1->Validar, 2->Registro
    include("conexion.php");
    //Comprobamos si el email es correcto
    if ((strlen($email)==0))
        $respuesta = array('res'=>-1,'desc'=>'email incorrecto');
    else {
        //Comprobamos si el email existe actualmente:
        $sql="select * from narraciones where us_email='$email'";
        $datos = mysql_query($sql,$conexion);
        if ($fila=mysql_fetch_assoc($datos))
            $usuario = new CVotos($fila["us_cod"],$fila["us_email"],$fila["us_votos"]);
        else
            $usuario = new CUsuario(0,"","");
        mysql_free_result($datos);enter code here
 
        switch ($op){
            case 1: //Validar:
                //Comprobamos si el pass es correcto:
                if ($usuario->getVotos()==$votos)
                    $respuesta=array('res'=>1,'op'=>$op,'desc'=>'OK');
                else
                    $respuesta=array('res'=>-1,'op'=>$op,'desc'=>'email o pass incorrectos');
                break;
            case 2: //Registro
                //Comprobamos si IDs coinciden:
 
                if ($usuario->getCod()==$cod) {
                    $sql="insert into narraciones (us_votos) values ('$votos')";
                    if (mysql_query($sql,$conexion)) {
 
                        $respuesta=array('res'=>1,'op'=>$op,'desc'=>'Votacion OK');
                    } else
                        $respuesta=array('res'=>-1,'op'=>$op,'desc'=>'Error al votar');
                }
                break;
            default:
                $respuesta=array('res'=>-1,'op'=>$op,'desc'=>'Operación no válida');
                break;
        }
    }
} else {
    $respuesta=array('res'=>-1,'op'=>$op,'desc'=>'No se ha podido leer objeto JSON');
}
} catch (Exception $e){
$respuesta=array('res'=>-1,'op'=>$op,'desc'=>$e->getMessage());
}
 
print(utf8_encode(json_encode($respuesta)));
?>

Si alguien ve el problema que lo diga por favor.

Un saludete!!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Value null of type org.json.JSONObject$1 cannot be converted to JSONObject Android

Publicado por snti (14 intervenciones) el 15/06/2015 21:33:27
Sabes usar el debugger?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar