1

i want to create a simple login. But I'm always getting a "org.json.JSONException: Value type java.lang.String cannot be converted to JSONArray error (read headline for complete Error)

The logcat:

 11-21 12:33:34.424 2427-2427/package+appname W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
    11-21 12:33:37.158 2427-2427/package+appname W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
    11-21 12:33:37.158 2427-2427/package+appname W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
    11-21 12:33:40.420 2427-2681/package+appname D/NetworkSecurityConfig: No Network Security Config specified, using platform default

I tested my response of the php file, this was the result: [{"code":"login_success","id":"3","name":"Karl","password":"qaz","email":"iop"}]

During debugging i saw he get some problems after "AppController.getInstance().addToRequestQueue(req);" but I'm not conform for what we are using the AppController class. My question is, what I'm missing? Thank you very much for any help.

My LoginRegister Acitvity:

public class LoginRegister extends AppCompatActivity {

final String TAG = this.getClass().getSimpleName();
Button login;
EditText email, pw;
String DataEmail, DataPw;
TextView tvE, tvP;
String ResponseID, ResponseName, ResponsePW, ResponseEMail;

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

    login = (Button) findViewById(R.id.btnLogin);

    email = (EditText) findViewById(R.id.etEmail);
    pw = (EditText) findViewById(R.id.etPassword);

    tvE = (TextView) findViewById(R.id.tvName);
    tvP = (TextView) findViewById(R.id.tvPW);
 }

public void main_register(View v) {
    onPause();
    startActivity(new Intent(this, Register.class));
}

public void loginClicked(View v) {
    DataEmail = email.getText().toString();
    DataPw = pw.getText().toString();

    String url = "http://10.0.2.2/getinfoCAF.php";
    //String url = "http://10.0.3.2/getinfoCAF.php";
    //String url = "http://192.168.1.128/getinfoCAF.php";
    makeJsonArrayRequest(url);
}

private void makeJsonArrayRequest(String url)
{
    JsonArrayRequest req = new JsonArrayRequest(url, new Response.Listener<JSONArray>()
    {
        @Override
        public void onResponse(JSONArray response) {
            Log.d(TAG, response.toString());

            try {
                String jsonResponse = "";
                for (int i = 0; i < response.length(); i++) {
                    JSONObject person = (JSONObject) response.get(i);
                    String success = person.getString("code");
                    String id = person.getString("id");
                    String name = person.getString("name");
                    String pw = person.getString("password");
                    String email = person.getString("email");

                    jsonResponse += "Success: " + success + "\n\n";
                    jsonResponse += "Name: " + name + "\n\n";
                    jsonResponse += "Email: " + email + "\n\n";
                    jsonResponse += "ID: " + id + "\n\n";
                    jsonResponse += "Mobile: " + pw + "\n\n\n";
                }

                tvE.setText(jsonResponse);

            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    }, new Response.ErrorListener()
    {
        @Override
        public void onErrorResponse(VolleyError error)
        {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(), error.getMessage(),   Toast.LENGTH_LONG).show();
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("email", DataEmail);
            hashMap.put("password", DataPw);

            return hashMap;
        }
        /*
        protected Map<String, String> getParams()
        {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("email", DataEmail);
            hashMap.put("password", DataPw);

            return hashMap;
        }
        */
    };
    AppController.getInstance().addToRequestQueue(req);
}}

My AppController Activity:

public class AppController extends Application {
public static final String TAG = AppController.class
        .getSimpleName();

private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;

private static AppController mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
}

public static synchronized AppController getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

    return mRequestQueue;
}

public ImageLoader getImageLoader() {
    getRequestQueue();
    if (mImageLoader == null) {
        mImageLoader = new ImageLoader(this.mRequestQueue,
                new LruBitmapCache());
    }
    return this.mImageLoader;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    // set the default tag if tag is empty
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);
}

public void cancelPendingRequests(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
}}     `

PHP Snippet:
`

$row = mysqli_fetch_row($result);
$id = $row[0];
$name = $row[1];
$password = $row[2];
$email2 = $row[3];  
$code = "login_success";
array_push($response, array("code"=>$code, "id"=>$id,"name"=>$name, "password"=>$password,"email"=>$email2));
echo json_encode($response);     `

My Manifest.xml

  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

  <application
    android:name=".AppController"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".LoginRegister">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".Register">

    </activity>

 </application>

I don't know what I'm doing wrong or missing.

EDIT The problem is that the server is getting null as parameters typedin in the EditTextfields.

SOLUTION

@Override
public Map<String, String> getParams() 
{
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("email", DataEmail);
        hashMap.put("password", DataPw);

        return hashMap;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError 
{
    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap.put("Content-Type", "application/x-www-form-urlencoded");

    return hashMap;
}

I need two hashmaps first with the data and the 2cnd with extra information what I'm sending.

booky
  • 31
  • 1
  • 3
  • By checking logcat it looks like a problem with your `EditText` or `TextView`. Check similar StackOverflow threads [this](http://stackoverflow.com/questions/8122625/getextractedtext-on-inactive-inputconnection-warning-on-android) and [this](http://stackoverflow.com/questions/12318521/inputconnectionwrapper-warning). `AppController` is a Singleton class. Here it's used for Volley network request. See [here](https://developer.android.com/training/volley/requestqueue.html#singleton) – Shashanth Nov 21 '16 at 16:58
  • If you type `http://10.0.2.2/getinfoCAF.php` in your browser if it's returned with `JSON` your server response is OK, but if it's returned with any HTML page that's the culprit over here. Because in Android part, you're actually expecting JSON. – Shashanth Nov 21 '16 at 17:04
  • If I'm typing the correct email and pw im getting no html, just [{"code":"login_success","id":"3","name":"Karl","password":"qaz","email":"iop"}]. But I can't see my response in the logcat... – booky Nov 21 '16 at 18:06
  • Even if I misstype the email or pw im getting json response. But still "D/Volley: [1] 2.onErrorResponse: LoginRegister" (Thread Title Error) – booky Nov 21 '16 at 18:15
  • 1
    You're not seeing your response in the LogCat because it's not ever getting into your onResponse callback. Try changing your JSONArrayRequest to a StringRequest so you can see what data you're getting back. – LukeWaggoner Nov 21 '16 at 18:41
  • ah yes now i can see the response thank you. – booky Nov 21 '16 at 18:58
  • Thats realy strange, it seems that my data transmitting from device to server is not working, because if im testing my php file by using getinfoCAF_test.html from my simulated device or from my pc i get as response an json array, but if im typing in my application i get the error as response that he couldnt find the user i typed in. – booky Nov 21 '16 at 19:21
  • If you got the answer post a [self-answer](http://stackoverflow.com/help/self-answer) for your question. – Shashanth Nov 22 '16 at 12:48

0 Answers0