简体   繁体   中英

How to retrieve values from mysql database in android studio with java or php using RETROFIT library?

I am trying to fetch values from database in android studio textview php for db connection.I'm using Retrofit library. Below is the code I've written to GET the values ... I'm getting this error "Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $"

 public class MainActivity extends AppCompatActivity {
TextView nametxt, agetxt, phonetxt, emailtxt;
Button retrieveBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    nametxt = (TextView) findViewById(R.id.nametxt);
    agetxt = (TextView) findViewById(R.id.agetxt);
    phonetxt = (TextView) findViewById(R.id.phonetxt);
    emailtxt = (TextView) findViewById(R.id.emailtxt);
    retrieveBtn = (Button) findViewById(R.id.retrieveBtn);
    retrieveBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fetchData();
        }
    });}


private void fetchData() {
    Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://10.0.2.2:8080/demo/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
   Api api = retrofit.create(Api.class);

    Call<List<Details_Pojo>> call = api.getstatus();

    call.enqueue(new Callback<List<Details_Pojo>>() {
        @Override
        public void onResponse(Call<List<Details_Pojo>> call, Response<List<Details_Pojo>> response) {


            List<Details_Pojo> adslist = response.body();

            String name = adslist.get(0).getName();
            String age = adslist.get(0).getAge();
            String phone = adslist.get(0).getPhone();
            String email = adslist.get(0).getEmail();

            nametxt.setText(name);
            agetxt.setText(age);
            phonetxt.setText(phone);
            emailtxt.setText(email);

        }

        @Override
        public void onFailure(Call<List<Details_Pojo>> call, Throwable t) {

            Toast.makeText(MainActivity.this, ""+t.getMessage().toString(), Toast.LENGTH_LONG).show();

        }
    });
}}

I've created API.java interface file

public interface Api {

String BASE_URL = "http://10.0.2.2:8080/demo/";
 @GET("fetch_data.php")
Call<List<Details_Pojo>> getstatus();

}

检查您的基本 URL .baseUrl(" http://10.0.2.2:8080/demo/ ") 并将其更改为 .baseUrl(" http://10.0.2.2/demo/ ")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM