Search code examples
phpandroidjsonandroid-studiobackground-task

Not getting json response from online server


So I came over this tutorial right here https://www.youtube.com/watch?v=mdAXqQoADt8 (last part of it) everything is working absolutly fine when I'm refering to my local server.

Now I switched it up to an online server/database to test if it works the same way. Also here everything is working fine except of the json response in the onPostExecute Method. I'm getting no messages from registration or login anymore. I really can't figure out how to solve this problem and I don't understand why it doesn't work the same way when I switch it up to an online server/database.

Hope anyone here can help me with that. I'll post some code below, maybe somebody can figure it out without doing the whole tutorial.

Thankful for any help!

@Override
    protected void onPostExecute(String json) {

        try {
            Log.d("JSON-String",json+"");
            progressDialog.dismiss();
            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray = jsonObject.getJSONArray("server_response");
            JSONObject JO = jsonArray.getJSONObject(0);
            String code = JO.getString("code");
            String message = JO.getString("message");
            if (code.equals("reg_true"))
            {
                showDialog("Registrierung erfolgt.", message, code);
            }
            else if (code.equals("reg_false"))
            {
                showDialog("Registrierung fehlgeschlagen", message, code);
            }

            else if(code.equals("login_true"))
            {
                Intent intent = new Intent(activity, Talkscreen.class);                                        //SPÄTER WIEDER RAUSNEHMEN
                activity.startActivity(intent);
                activity.finish();
            }
            else if (code.equals("login_false"))
            {
                showDialog("Login fehlgeschlagen", message,code);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

Solution

  • It seems you're getting wrong JSON string from the server.

    2905-2905/com.appmac.ron.testapp W/System.err: org.json.JSONException: Value <h3>DB< of type java.lang.String cannot be converted to JSONObject
    04-23 12:35:53.172 2905-2905/com.appmac.ron.testapp W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
    04-23 12:35:53.172 2905-2905/com.appmac.ron.testapp W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:160)
    04-23 12:35:53.172 2905-2905/com.appmac.ron.testapp W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
    04-23 12:35:53.172 2905-2905/com.appmac.ron.testapp W/System.err:     at com.appmac.ron.testapp.ServerKlassen.BackgroundTask.onPostExecute(BackgroundTask.java:172)
    04-23 12:35:53.172 2905-2905/com.appmac.ron.testapp W/System.err:     at com.appmac.ron.testapp.ServerKlassen.BackgroundTask.onPostExecute(BackgroundTask.java:39)
    04-23 12:35:53.172 2905-2905/com.appmac.ron.testapp W/System.err:     at android.os.AsyncTask.finish(AsyncTask.java:651)
    04-23 12:35:53.172 2905-2905/com.appmac.ron.testapp W/System.err:     at android.os.AsyncTask.-wrap1(AsyncTask.java)
    04-23 12:35:53.172 2905-2905/com.appmac.ron.testapp W/System.err:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
    04-23 12:35:53.172 2905-2905/com.appmac.ron.testapp W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
    04-23 12:35:53.172 2905-2905/com.appmac.ron.testapp W/System.err:     at android.os.Looper.loop(Looper.java:148)
    04-23 12:35:53.172 2905-2905/com.appmac.ron.testapp W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
    04-23 12:35:53.172 2905-2905/com.appmac.ron.testapp W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    04-23 12:35:53.172 2905-2905/com.appmac.ron.testapp W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    04-23 12:35:53.172 2905-2905/com.appmac.ron.testapp W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    

    Above error shows that String could not be converted to JSON object.

    Check the server code for any bug.