0

I created a python flask web service and deployed it on localhost:5000 using apache, which returns data in JSON. Now i have to write an android app to call the web service and return the data. But as soon as I run the application, emulator crashes and reopen. Then sometimes the app opens and when i click CONNECT, goes to onFailure().

GetJobs.java (interface)-

package com.example.jobs4;

import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.http.GET;

public interface GetJobs {
    @GET("/getjobs")
    Call<ArrayList<RetroJobs>> getAllJobs();
}

RetroJobs.java (data model)-

package com.example.jobs4;

import com.google.gson.annotations.SerializedName;

public class RetroJobs {
    @SerializedName("name")
    private String name;
    @SerializedName("id")
    private String id;
    @SerializedName("run_start")
    private String run_start;
    @SerializedName("run_end")
    private String run_end;
    @SerializedName("scheduled_date")
    private String scheduled_date;
    @SerializedName("status")
    private String status;

    public RetroJobs(String name, String id, String scheduled_date, String run_start, String run_end, String status) {
        this.name = name;
        this.scheduled_date = scheduled_date;
        this.run_start = run_start;
        this.run_end = run_end;
        this.id = id;
        this.status = status;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public String getScheduled_date() {
        return scheduled_date;
    }
    public void setScheduled_date(String scheduled_date) {
        this.scheduled_date = scheduled_date;
    }

    public String getRun_start() {
        return run_start;
    }
    public void setRun_start(String run_start) {
        this.run_start = run_start;
    }

    public String getRun_end() {
        return run_end;
    }
    public void setRun_end(String run_end) {
        this.run_end = run_end;
    }

    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
}

RetrofitClient.java (retrofit instance)-

package com.example.jobs4;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {
    private static Retrofit retrofit;
    private static final String BASE_URL = "http://10.0.2.2:5000";

    public static Retrofit getRetrofitInstance() {
        if (retrofit == null) {
            retrofit = new retrofit2.Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

MainActivity.java-

package com.example.jobs4;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

    ListView listView;
    ArrayList<RetroJobs> list;
    ArrayAdapter<RetroJobs> adapter;

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

    public void startCon(View view){

        GetJobs service = RetrofitClient.getRetrofitInstance().create(GetJobs.class);

        Call<ArrayList<RetroJobs>> call = service.getAllJobs();
        call.enqueue(new Callback<ArrayList<RetroJobs>>() {

            @Override
            public void onResponse(Call<ArrayList<RetroJobs>> call, Response<ArrayList<RetroJobs>> response) {
                listView = findViewById(R.id.lv);
                list = new ArrayList<>();
                list = response.body();
                adapter = new ArrayAdapter<RetroJobs>(MainActivity.this,android.R.layout.simple_list_item_1,list);
                adapter.notifyDataSetChanged();
                listView.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<ArrayList<RetroJobs>> call, Throwable throwable) {
                Toast.makeText(MainActivity.this, "Oops! Unable to load!", Toast.LENGTH_SHORT).show();
            }
        });
    }

}

I've tested my web service using postman. I opened the URL on emulator directly and returns the json data correctly. This is what it returns-

[{"id":19,"name":"temp_st_ser","run_end":"13:20:10","run_start":"13:00:15","scheduled_date":"2019-08-04","status":0},{"id":11,"name":"st_sch","run_end":"10:24:17","run_start":"10:10:05","scheduled_date":"2019-05-14","status":1},{"id":5,"name":"sch_main","run_end":"11:44:22","run_start":"11:12:00","scheduled_date":"2019-03-09","status":0}]

Edit: Now all of a sudden its opening but giving this output on the listview -

com.example.jobs4.RetroJobs@ebffc7e 

com.example.jobs4.RetroJobs@e2215df

com.example.jobs4.RetroJobs@23a722c

Logcat-

2019-06-07 16:57:58.399 14731-14731/com.example.jobs4 I/zygote:     at void com.example.jobs4.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:26)
2019-06-07 16:57:58.399 14731-14731/com.example.jobs4 I/zygote:     at void android.app.Activity.performCreate(android.os.Bundle, android.os.PersistableBundle) (Activity.java:7009)
2019-06-07 16:57:58.399 14731-14731/com.example.jobs4 I/zygote:     at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:7000)
2019-06-07 16:57:58.399 14731-14731/com.example.jobs4 I/zygote:     at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1214)
2019-06-07 16:57:58.399 14731-14731/com.example.jobs4 I/zygote:     at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2731)
2019-06-07 16:57:58.399 14731-14731/com.example.jobs4 I/zygote:     at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2856)
2019-06-07 16:57:58.399 14731-14731/com.example.jobs4 I/zygote:     at void android.app.ActivityThread.-wrap11(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
2019-06-07 16:57:58.399 14731-14731/com.example.jobs4 I/zygote:     at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1589)
2019-06-07 16:57:58.399 14731-14731/com.example.jobs4 I/zygote:     at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:106)
2019-06-07 16:57:58.400 14731-14731/com.example.jobs4 I/zygote:     at void android.os.Looper.loop() (Looper.java:164)
2019-06-07 16:57:58.400 14731-14731/com.example.jobs4 I/zygote:     at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6494)
2019-06-07 16:57:58.400 14731-14731/com.example.jobs4 I/zygote:     at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2)
2019-06-07 16:57:58.400 14731-14731/com.example.jobs4 I/zygote:     at void com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run() (RuntimeInit.java:438)
2019-06-07 16:57:58.400 14731-14731/com.example.jobs4 I/zygote:     at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:807)
2019-06-07 16:57:58.481 14731-14748/com.example.jobs4 D/OpenGLRenderer: HWUI GL Pipeline
2019-06-07 16:57:58.542 14731-14748/com.example.jobs4 I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2019-06-07 16:57:58.542 14731-14748/com.example.jobs4 I/OpenGLRenderer: Initialized EGL, version 1.4
2019-06-07 16:57:58.542 14731-14748/com.example.jobs4 D/OpenGLRenderer: Swap behavior 1
2019-06-07 16:57:58.542 14731-14748/com.example.jobs4 W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2019-06-07 16:57:58.542 14731-14748/com.example.jobs4 D/OpenGLRenderer: Swap behavior 0
2019-06-07 16:57:58.573 14731-14748/com.example.jobs4 D/EGL_emulation: eglCreateContext: 0xea62c280: maj 2 min 0 rcv 2
2019-06-07 16:57:58.576 14731-14748/com.example.jobs4 D/EGL_emulation: eglMakeCurrent: 0xea62c280: ver 2 0 (tinfo 0xdfeff2a0)
2019-06-07 16:57:58.662 14731-14748/com.example.jobs4 D/EGL_emulation: eglMakeCurrent: 0xea62c280: ver 2 0 (tinfo 0xdfeff2a0)
2019-06-07 16:58:08.418 14731-14731/com.example.jobs4 D/NetworkSecurityConfig: No Network Security Config specified, using platform default

Complete Logcat- https://drive.google.com/drive/folders/1K-TcyzeWqlNghOtBsLMNMQruViDzYfCC?usp=sharing

davidism
  • 121,510
  • 29
  • 395
  • 339
Abhimanyu
  • 41
  • 1
  • 7
  • 1
    "But as soon as I run the application, emulator crashes and reopen." -- use Logcat to examine the stack trace associated with your crashes: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Jun 07 '19 at 11:01
  • 1
    Post your entire stack trace – Saurabh Thorat Jun 07 '19 at 11:21
  • Doing that pushes me over the total characters limit. Can't post it. – Abhimanyu Jun 07 '19 at 11:30
  • "Doing that pushes me over the total characters limit" -- then cut back on other things. For example, we are unlikely to be able to use the Flask code. You could also get rid of extraneous sentences like "I've searched various tutorials on consuming APIs using retrofit, but no use". "but giving this output on the listview" -- you created a plain `ArrayAdapter`. It will just call `toString()` on the model object and display that value in the list. You did not override `toString()` or create a custom `ArrayAdapter` subclass. So, you get the stock `Object` `toString()` result. – CommonsWare Jun 07 '19 at 11:33
  • How to create a custom ````ArrayAdapter```` class? – Abhimanyu Jun 07 '19 at 12:33

0 Answers0