我正在尝试从android studio textview php数据库中获取数据库连接的值。我正在使用改装库。下面是我为获取值而编写的代码。。。我得到这个错误“使用jsonreader.setlenient(true)在第1行第1列路径$接受格式错误的json”
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();
}
});
}}
我已经创建了api.java接口文件
公共接口api{
String BASE_URL = "http://10.0.2.2:8080/demo/";
@GET("fetch_data.php")
Call<List<Details_Pojo>> getstatus();
}
1条答案
按热度按时间ia2d9nvy1#
检查基本url。基本url(“http://10.0.2.2:8080/demo/“),并将其更改为.baseurl(”http://10.0.2.2/demo/")