this is my java code and i am using volley and there are no errors in the code and the api works correctly when put in post man but when it come to android and i post my data it only shows one result yet in post man am able to see two results
package com.tonnykratos.heartdiseasepredictionz;
import androidx.appcompat.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.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.HTTP;
public class MainActivity extends AppCompatActivity {
EditText age,sex,cp,trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal;
Button predict;
TextView result;
String url = "https://heartdisease-predictionz.herokuapp.com/predict";
private JsonPlaceHolderApi jsonPlaceHolderApi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
age = findViewById(R.id.age);
sex = findViewById(R.id.sex);
cp = findViewById(R.id.cp);
trestbps = findViewById(R.id.trestbps);
chol = findViewById(R.id.chol);
fbs = findViewById(R.id.fbs);
restecg = findViewById(R.id.restecg);
thalach = findViewById(R.id.thalach);
exang = findViewById(R.id.exang);
oldpeak = findViewById(R.id.oldpeak);
slope = findViewById(R.id.slope);
ca = findViewById(R.id.ca);
thal = findViewById(R.id.thal);
predict = findViewById(R.id.predict);
result = findViewById(R.id.result);
// Gson gSon = new GsonBuilder().serializeNulls().create();
//
// HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
// loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//
// OkHttpClient okHttpClient = new OkHttpClient.Builder()
// .addInterceptor(loggingInterceptor)
// .build();
//
// Retrofit retrofit = new Retrofit.Builder()
// .baseUrl("https://heartdisease-predictionz.herokuapp.com/")
// .addConverterFactory(GsonConverterFactory.create())
// .client(okHttpClient)
// .build();
// jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
// //getPosts();
// createPost();
predict.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
//getPosts();
//createPost();
//hit the api with volley
//
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String data = jsonObject.getString("HeartDisease");
result.setText(data);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
}
){
@Override
protected Map<String, String> getParams(){
Map<String, String> params =new HashMap<>();
params.put("age",age.getText().toString());
params.put("sex", sex.getText().toString());
params.put("cp", cp.getText().toString());
params.put("trestbps", trestbps.getText().toString());
params.put("chol", chol.getText().toString());
params.put("fbs", fbs.getText().toString());
params.put("restecg", restecg.getText().toString());
params.put("thalach", thalach.getText().toString());
params.put("exang", exang.getText().toString());
params.put("oldpeak", oldpeak.getText().toString());
params.put("slope", slope.getText().toString());
params.put("ca", ca.getText().toString());
params.put("thal", thal.getText().toString());
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
queue.add(stringRequest);
}
});
}
private void getPosts(){
Call<List<Post>> call = jsonPlaceHolderApi.getPosts();
call.enqueue(new Callback<List<Post>>() {
@Override
public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
if (!response.isSuccessful()){
result.setText("code: " + response.code());
return;
}
List<Post> posts = response.body();
for (Post post : posts){
String content = "";
content += "HeartDisease: "+ post.getTitle() + "/n";
result.append(content);
}
}
@Override
public void onFailure(Call<List<Post>> call, Throwable t) {
result.setText(t.getMessage());
}
});
}
private void createPost(){
// Post post = new Post(
// age.getText().toString(),
// sex.getText().toString(),
// cp.getText().toString(),
// trestbps.getText().toString(),
// chol.getText().toString(),
// fbs.getText().toString(),
// restecg.getText().toString(),
// thalach.getText().toString(),
// exang.getText().toString(),
// oldpeak.getText().toString(),
// slope.getText().toString(),
// ca.getText().toString(),
// thal.getText().toString()
// );
Map<String, String> fields = new HashMap<>();
fields.put("age",age.getText().toString());
fields.put("sex", sex.getText().toString());
fields.put("cp", cp.getText().toString());
fields.put("trestbps", trestbps.getText().toString());
fields.put("chol", chol.getText().toString());
fields.put("fbs", fbs.getText().toString());
fields.put("restecg", restecg.getText().toString());
fields.put("thalach", thalach.getText().toString());
fields.put("exang", exang.getText().toString());
fields.put("oldpeak", oldpeak.getText().toString());
fields.put("slope", slope.getText().toString());
fields.put("ca", ca.getText().toString());
fields.put("thal", thal.getText().toString());
Call<Post> call = jsonPlaceHolderApi.createPost(fields);
call.enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
if (!response.isSuccessful()){
result.setText("code: " + response.code());
return;
}
Post postResponse = response.body();
String content = "";
content += postResponse.toString();
result.setText(content);
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
result.setText(t.getMessage());
}
});
}
}
and also this is where i created my api from and hosted it ar heroku
from unittest import result
from flask import Flask,request,jsonify
import pickle
import numpy as np
model = pickle.load(open('model.pkl','rb'))
app = Flask (__name__)
@app.route('/')
def home():
return "Hello World"
@app.route('/predict', methods=['POST'])
def predict():
model = pickle.load(open('model.pkl','rb'))
int_features = [float(x) for x in request.form.values()]
features = [np.array(int_features)]
diagnosis = {0: 'Your Result is Normal',
1: 'HeartDisease Detected'
}
prediction = model.predict(features)[0]
result = diagnosis.get(prediction)
return jsonify({'HeartDisease': str(result)})
if __name__ == '__main__':
app.run(debug=True)
whenever i run the code am always getting one result in the json file