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.