我正在开发一个应用程序,不得不提出一个API请求并在RecyclerView
中显示数据(我使用了改装)。完成代码后,我发现数据提取是HTTPS APIs
可以正常工作,但是当我在HTTP API
中放入BASE_URL
时,它不起作用。
以下API可以正常使用: https://demonuts.com/Demonuts/JsonTest/Tennis/json_parsing.php
但是使用HTTP API
时,不会显示输出。
MainActivity
package com.example.parsaniahardik.json_recycler_retrofit;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;
public class MainActivity extends AppCompatActivity {
private RetrofitAdapter retrofitAdapter;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler);
fetchJSON();
}
private void fetchJSON(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(RecyclerInterface.JSONURL)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
RecyclerInterface api = retrofit.create(RecyclerInterface.class);
Call<String> call = api.getString();
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
Log.i("Responsestring", response.body().toString());
//Toast.makeText()
if (response.isSuccessful()) {
if (response.body() != null) {
Log.i("onSuccess", response.body().toString());
String jsonresponse = response.body().toString();
writeRecycler(jsonresponse);
} else {
Log.i("onEmptyResponse", "Returned empty response");//Toast.makeText(getContext(),"Nothing returned",Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
private void writeRecycler(String response){
try {
//getting the whole json object from the response
JSONObject obj = new JSONObject(response);
if(obj.optString("status").equals("true")){
ArrayList<ModelRecycler> modelRecyclerArrayList = new ArrayList<>();
JSONArray dataArray = obj.getJSONArray("data");
for (int i = 0; i < dataArray.length(); i++) {
ModelRecycler modelRecycler = new ModelRecycler();
JSONObject dataobj = dataArray.getJSONObject(i);
modelRecycler.setName(dataobj.getString("name"));
modelRecycler.setCountry(dataobj.getString("country"));
modelRecycler.setCity(dataobj.getString("city"));
modelRecyclerArrayList.add(modelRecycler);
}
retrofitAdapter = new RetrofitAdapter(this,modelRecyclerArrayList);
recyclerView.setAdapter(retrofitAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
}else {
Toast.makeText(MainActivity.this, obj.optString("message")+"", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
适配器
public class RetrofitAdapter extends RecyclerView.Adapter<RetrofitAdapter.MyViewHolder> {
private LayoutInflater inflater;
private ArrayList<ModelRecycler> dataModelArrayList;
public RetrofitAdapter(Context ctx, ArrayList<ModelRecycler> dataModelArrayList){
inflater = LayoutInflater.from(ctx);
this.dataModelArrayList = dataModelArrayList;
}
@Override
public RetrofitAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.retro_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RetrofitAdapter.MyViewHolder holder, int position) {
holder.name.setText(dataModelArrayList.get(position).getName());
holder.country.setText(dataModelArrayList.get(position).getCountry());
holder.city.setText(dataModelArrayList.get(position).getCity());
}
@Override
public int getItemCount() {
return dataModelArrayList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView country, name, city;
public MyViewHolder(View itemView) {
super(itemView);
country = (TextView) itemView.findViewById(R.id.country);
name = (TextView) itemView.findViewById(R.id.name);
city = (TextView) itemView.findViewById(R.id.city);
}
}
}
ApiInterface
public interface RecyclerInterface {
String JSONURL = "https://demonuts.com/Demonuts/JsonTest/Tennis/";
@GET("json_parsing.php")
Call<String> getString();
}
ModelClass
public class ModelRecycler {
private String name, country, city;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
我想让我的API提取既可用于HTTPS APIs
也可用于HTTP APIs
答案 0 :(得分:0)
已解决: 如@ DEVV911所述,如果您在Android 9(Pie)上测试应用程序,则会遇到此问题。 如果您遇到相同的问题,则只需在res文件夹中创建一个新的Android资源目录,并将其命名为“ xml”,资源类型为XML。在“ xml”文件夹中,以“ network_security_config”的名称创建一个新的XML资源文件。在其中输入
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">YourDomainName.com</domain>
</domain-config>
</network-security-config>
之后,转到清单文件并在应用程序标记内键入:
<android:networkSecurityConfig="@xml/network_security_config"
然后工作正常。