How to fetch data from mysql in android using retrofit

北战南征 提交于 2021-01-29 07:41:54

问题


actually i want to fetch the data from mysql using retrofit in android. This is my scenario I am passing a string data from one activity to another which is fine now i want to use this variable to select the same data in database for e.g my string has a value of 2 now i want see in database wether the same 2 is in db or not like it some kind of seacrhing.

Here is mysql code


<?php
    $servername="localhost";
    $username="root";
    $password="";
    $dbname="academia";

$conn=mysqli_connect($servername,$username,$password,$dbname);
error_reporting(0);
$connDB= mysqli_select_db($conn,'academia');
if ($_SERVER['REQUEST_METHOD']=='POST') {
   $mobile = $_POST['Mobile_Number'];
    $sql = "SELECT Mobile_Number FROM school_signup WHERE Mobile_Number='$mobile' ";

    $response = mysqli_query($conn, $sql);

    $data = mysqli_num_rows($response);
    if($data>0)
{
        $error='ok';

        echo json_encode(array('response'=>$error,'Mobile_Number'=>$mobile));

}else{
$error = 'failed';
echo json_encode(array('response'=>$error));    
}
}

?>

Now here is the code of Apinterface.java

import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
import retrofit2.http.Query;

public interface Apinterface {
   // @FormUrlEncoded
    @POST("login.php")

    Call<MobileNumber> sendnum(@Query("Mobile_Number") String Mobile_Number);

}

Now here is code of ApiClient.java

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

public class ApiClient {
    private static final String BASE_URL = "http://192.168.10.4/android_register_login/";
    private static Retrofit retrofit;
    private static ApiClient mInstance;

    public static Retrofit getApiClient() {

            retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();

        return retrofit;
    }

}

Here is file to initialize variables


public class MobileNumber {

    @SerializedName("response")
    @Expose
    private String response;
    @SerializedName("Mobile_Number")
    @Expose
    private String mobileNumber;

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

}

Here is my mainactivity file

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_school_mobile_code);
        apiInterface = ApiClient.getApiClient().create(Apinterface.class);
}
 private void sendnum(){
        Bundle bundle = getIntent().getExtras();
        final String Mobile_Number= bundle.getString("Mobile");
        Call<MobileNumber> call =apiInterface.sendnum(Mobile_Number);
        call.enqueue(new Callback<MobileNumber>() {
            @Override
            public void onResponse(Call<MobileNumber> call, Response<MobileNumber> response) {
                if(response.body().getResponse().equals("ok")){
                    Toast.makeText(SchoolMobileCode.this, "Success", Toast.LENGTH_SHORT).show();

                }
                else if(response.body().getResponse().equals("failed")){
                    Toast.makeText(SchoolMobileCode.this, "Error", Toast.LENGTH_SHORT).show();

                }
            }
            @Override
            public void onFailure(Call<MobileNumber> call, Throwable t) {
                //display errror message
                Toast.makeText(SchoolMobileCode.this, "F"+t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

    }

One thing more response coming from json is absloutly fine the problem is in "Mobile_Number" variable which i am send to to do same search

来源:https://stackoverflow.com/questions/57257201/how-to-fetch-data-from-mysql-in-android-using-retrofit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!