0

I want to call url where PHP Script written on server that return me error or success after comparing username password but when i click on submit button it gives me this error

com.pace.testformwithonline E/Response error: 
com.android.volley.ParseError: org.json.JSONException: Value Error of type 
java.lang.String cannot be converted to JSONObject

package com.pace.testformwithonline;

import android.app.Activity;
import android.app.DownloadManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends Activity {
    private EditText username;
    private EditText password;
    private Button btnSubmit;
    private TextView result;
    private static final String URL="http://pace-tech.com/nomi/zaigham/android.php";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username=(EditText)findViewById(R.id.editText_username);
        password= (EditText) findViewById(R.id.editText_password);
        btnSubmit=(Button)findViewById(R.id.btn_submit);
        result=(TextView)findViewById(R.id.text_result);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText("");
            postJsonobjectRequest(URL);
            }

        });

    }
    public void postJsonobjectRequest(String url){
        JSONObject params=new JSONObject();
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        try {
            params.put("username", username.getText().toString());
            params.put("password", password.getText().toString());
        }
        catch (JSONException e){
         e.printStackTrace();
        }
        JsonObjectRequest jsOBJRequest=new JsonObjectRequest
                (Request.Method.POST, URL, params, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
               // Log.e("Response is",response.toString());
                try {
                    Log.e("Response is",response.toString());
                    String Success = response.getString("Success").toString();
                    String Message = response.getString("Message").toString();
                    if(Success=="0") {
                        result.setText(Message);
                    }
                    else {
                        Toast.makeText(getApplicationContext(),Message,Toast.LENGTH_LONG);
                    }


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

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Log.e("Response error",error.toString());
                result.setText("Error Is :"+ error.toString());
            }
        });
        queue.add(jsOBJRequest);
    }
}

Here Is PHP SCRIPT

<?php
$username = "zaigi";
$password = "1111";

$androidUserName = json_decode($_POST['username']);
$androidPassword = json_decode($_POST['password']);

if($androidUserName == $username && $androidPassword == $password){
 $response = "Correct";
}else{
 $response = "Error";
} 
echo json_encode($response);
?>
Zaigham Raza
  • 364
  • 2
  • 3
  • 14
  • Not exactly sure, but it looks like the php script responded with the just the string `Error`. – VolkerK Mar 03 '16 at 06:42
  • where this error occure, on what line – Konstantin Volkov Mar 03 '16 at 06:44
  • I am sending username and password through json object. In php it checks against username and password but it returns this line of error:com.pace.testformwithonline E/Response error: com.android.volley.ParseError: org.json.JSONException: Value Error of type java.lang.String cannot be converted to JSONObject. – Zaigham Raza Mar 03 '16 at 06:48
  • Can you please post the expected json response here? I think you are storing a json object value in the string. – Animesh Jena Mar 03 '16 at 06:49
  • Here is my Logcat: 03-03 11:49:32.092 2343-2343/com.pace.testformwithonline E/Response error: com.android.volley.ParseError: org.json.JSONException: Value Error of type java.lang.String cannot be converted to JSONObject – Zaigham Raza Mar 03 '16 at 06:50
  • Expected Result in encoded in json and it should return "Correct" or "Error" – Zaigham Raza Mar 03 '16 at 06:51
  • Is the php script short enough so you can post it here, too? (without any or with obfuscated database credentials if present in the script of course ;-) ) – VolkerK Mar 03 '16 at 06:52
  • PHP script added in question @VolkerK – Zaigham Raza Mar 03 '16 at 06:55

3 Answers3

0

This is the parsing Error, that means volley can't parse the response that coming from your server, so kindly check the response string, is it in proper JSON format or not? you can check your response string here if its a proper JSON or not.

0

i know this problem. it happens to me once. Use StringRequest instead of JsonObjectRequest . this is volly bug. params are not passing in JsonObjectRequest .

please refer this link for more Volley JsonObjectRequest Post request not working

you will get Response as String then convert it into JSONObject.

Community
  • 1
  • 1
Jaydeep Devda
  • 727
  • 4
  • 18
0

In PHP SCRIPT do it some thing like this..

    $data = file_get_contents('php://input');
    $json = json_decode($data);
    //username and password sent from android
    $username=$json->{'username'};
    $password=$json->{'password'};


    $androidUserName = $username;
    $androidPassword = $password;

    if($androidUserName == $myusername && $androidPassword == $mypassword){
        $response = array('result' => "Correct");
    }else{
        $response = array('result' => $androidUserName,'password' => $androidPassword);
    }
    header('Content-type: application/json');
    echo json_encode($response);
K. Uzair
  • 303
  • 3
  • 16