How to consume JSON data in android from an API in Asp.net

0

Good evening, I'm trying to make a simple crud with android studio (2.3.3), but I can not return data. With postman I get good, but with android gets null. The site is link

Here'smycode:

publicclassCustomAdapterextendsArrayAdapter<Student>{publicCustomAdapter(Contextcontext,intresource,List<Student>student){super(context,resource,student);}@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){Viewv=convertView;if(v==null){LayoutInflaterinflater=(LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);v=inflater.inflate(R.layout.view_student_entry,parent,false);}Studentstudent=getItem(position);if(student!=null){TextViewtvStudentId=(TextView)v.findViewById(R.id.student_Id);TextViewtvStudentName=(TextView)v.findViewById(R.id.student_name);tvStudentId.setText(Integer.toString(student.Id));tvStudentName.setText(student.Name);}returnv;}}
publicinterfaceInstituteService{//RetrofitturnsourinstituteWEBAPIintoaJavainterface.//SothesearethelistavailableinourWEBAPIandthemethodslookstraightforward@GET("/api/Students")
    public void getStudent(Callback<List<Student>> callback);


    @GET("/api/Students/{id}")
    public void getStudentById(@Path("id") Integer id,Callback<Student> callback);


    @DELETE("/api/Students/{id}")
    public void deleteStudentById(@Path("id") Integer id,Callback<Student> callback);


    @PUT("/api/Students/{id}")
    public void updateStudentById(@Path("id") Integer id,@Body Student student,Callback<Student> callback);


    @POST("/api/Students")
    public void addStudent(@Body Student student,Callback<Student> callback);
}
public class MainActivity extends ActionBarActivity implements android.view.View.OnClickListener {

    ListView listView;
    Button btnGetAll_button,btnAdd_button;
    RestService restService;
    TextView student_Id_textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        restService = new RestService();
        setContentView(R.layout.activity_main);
        btnGetAll_button = (Button) findViewById(btnGetAll);
        btnGetAll_button.setOnClickListener(this);

        btnAdd_button= (Button) findViewById(R.id.btnAdd);
        btnAdd_button.setOnClickListener(this);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    //This function will call when the screen is activate
    @Override
    public void onResume() {

        super.onResume();
        refreshScreen();
    }

    @Override
    public void onClick(View v) {
        if (v== findViewById(R.id.btnAdd)){

            Intent intent = new Intent(this,StudentDetail.class);
            intent.putExtra("student_Id",0);
            startActivity(intent);

        }else {
            // You should use refreshScreen() instead, just show you an easier method only :P
            refreshScreen_SimpleWay();
        }
    }

    private void refreshScreen(){

        //Call to server to grab list of student records. this is a asyn
        restService.getService().getStudent(new Callback<List<Student>>() {
            @Override
            public void success(List<Student> students, Response response) {
                ListView lv = (ListView) findViewById(R.id.listView);

                CustomAdapter customAdapter = new CustomAdapter(MainActivity.this, R.layout.view_student_entry, students);

                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        student_Id_textView = (TextView) view.findViewById(student_Id);
                        String studentId = student_Id_textView.getText().toString();
                        Intent objIndent = new Intent(getApplicationContext(), StudentDetail.class);
                        objIndent.putExtra("student_Id", Integer.parseInt(studentId));
                        startActivity(objIndent);
                    }
                });
                lv.setAdapter(customAdapter);
            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(MainActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
            }
        });


    }


    private void refreshScreen_SimpleWay() {

        restService.getService().getStudent(new Callback<List<Student>>() {
            @Override
            public void success(List<Student> students, Response response) {
                ListView lv = (ListView) findViewById(R.id.listView);


                ArrayList<HashMap<String, String>> studentList = new ArrayList<HashMap<String, String>>();

                for (int i = 0; i < students.size(); i++) {
                    HashMap<String, String> student = new HashMap<String, String>();
                    student.put("id", String.valueOf(students.get(i).Id));
                    student.put("name", String.valueOf(students.get(i).Name));

                    studentList.add(student);
                }

                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        student_Id_textView = (TextView) view.findViewById(student_Id);
                        String studentId = student_Id_textView.getText().toString();
                        Intent objIndent = new Intent(getApplicationContext(), StudentDetail.class);
                        objIndent.putExtra("student_Id", Integer.parseInt(studentId));
                        startActivity(objIndent);
                    }
                });
                ListAdapter adapter = new SimpleAdapter(MainActivity.this, studentList, R.layout.view_student_entry, new String[]{"id", "name"}, new int[]{student_Id, R.id.student_name});
                lv.setAdapter(adapter);
            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(MainActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
            }
        });

    }
}
public class RestService {

    //http://gventurasistemas-001-site2.ctempurl.com/api/Students
    private static final String URL = "http://gventurasistemas-001-site2.ctempurl.com/";
    private retrofit.RestAdapter restAdapter;
    private InstituteService apiService;

    public RestService()
    {
        restAdapter = new retrofit.RestAdapter.Builder()
                .setEndpoint(URL)
                .setLogLevel(retrofit.RestAdapter.LogLevel.FULL)
                .build();

        apiService = restAdapter.create(InstituteService.class);
    }

    public InstituteService getService()
    {
        return apiService;
    }
}
public class StudentDetail extends ActionBarActivity implements android.view.View.OnClickListener {

    Button btnSave, btnDelete;
    Button btnClose;
    EditText editTextName;
    EditText editTextEmail;
    EditText editTextAge;
    private int _Student_Id=0;
    RestService restService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        restService = new RestService();
        setContentView(R.layout.activity_student_detail);

        btnSave = (Button) findViewById(R.id.btnSave);
        btnDelete = (Button) findViewById(R.id.btnDelete);
        btnClose = (Button) findViewById(R.id.btnClose);

        editTextName = (EditText) findViewById(R.id.editTextName);
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        editTextAge = (EditText) findViewById(R.id.editTextAge);

        btnSave.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnClose.setOnClickListener(this);


        _Student_Id =0;
        Intent intent = getIntent();
        _Student_Id =intent.getIntExtra("student_Id", 0);
        if (_Student_Id>0){
            restService.getService().getStudentById(_Student_Id, new Callback<Student>() {
                @Override
                public void success(Student student, Response response) {

                    editTextAge.setText(String.valueOf(student.Age));
                    editTextName.setText(student.Name);
                    editTextEmail.setText(student.Email);
                }

                @Override
                public void failure(RetrofitError error) {
                    Toast.makeText(StudentDetail.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                }
            });
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_student_detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        if (findViewById(R.id.btnDelete)==v){
            restService.getService().deleteStudentById(_Student_Id, new Callback<Student>() {
                @Override
                public void success(Student student, Response response) {
                    Toast.makeText(StudentDetail.this, "Student Record Deleted", Toast.LENGTH_LONG).show();
                }

                @Override
                public void failure(RetrofitError error) {
                    Toast.makeText(StudentDetail.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                }
            });

            finish();

        } else if (v== findViewById(R.id.btnClose)) {
            finish();
        } else if (findViewById(R.id.btnSave)==v) {

            Student student=new Student();
            Integer status =0;
            student.Email= editTextEmail.getText().toString();
            student.Name=editTextName.getText().toString();
            student.Age= Integer.parseInt(editTextAge.getText().toString());
            student.Id = _Student_Id;

            if (_Student_Id == 0) {
                restService.getService().addStudent(student, new Callback<Student>() {
                    @Override
                    public void success(Student student, Response response) {
                        Toast.makeText(StudentDetail.this, "New Student Inserted.", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void failure(RetrofitError error) {
                        Toast.makeText(StudentDetail.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();

                    }
                });

            } else {
                restService.getService().updateStudentById(_Student_Id,student , new Callback<Student>() {
                    @Override
                    public void success(Student student, Response response) {
                        Toast.makeText(StudentDetail.this, "Student Record updated.", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void failure(RetrofitError error) {
                        Toast.makeText(StudentDetail.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                    }
                });
            }
        }
    }
}

------------------ Active Main -------------

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.gventurasistemas.webapiclient.MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get All"
        android:id="@+id/btnGetAll"
        android:layout_alignBottom="@+id/listView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Student"
        android:id="@+id/btnAdd"
        android:layout_alignBottom="@+id/listView"
        android:layout_toRightOf="@+id/btnGetAll"
        android:layout_toEndOf="@+id/btnGetAll" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.gventurasistemas.webapiclient.StudentDetail">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Name"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="30dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Email"
        android:id="@+id/textView2"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="29dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Age"
        android:id="@+id/textView3"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="29dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/editTextName"
        android:layout_above="@+id/textView2"
        android:layout_toRightOf="@+id/textView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/editTextEmail"
        android:layout_above="@+id/textView3"
        android:layout_toRightOf="@+id/textView"
        android:layout_alignRight="@+id/editTextName"
        android:layout_alignEnd="@+id/editTextName" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/editTextAge"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignLeft="@+id/editTextEmail"
        android:layout_alignStart="@+id/editTextEmail"
        android:layout_alignRight="@+id/editTextEmail"
        android:layout_alignEnd="@+id/editTextEmail" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:id="@+id/btnSave"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/btnClose" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Close"
        android:id="@+id/btnClose"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete"
        android:id="@+id/btnDelete"
        android:layout_alignTop="@+id/btnSave"
        android:layout_toLeftOf="@+id/btnSave" />
</RelativeLayout>
    
asked by anonymous 26.06.2018 / 23:42

0 answers