我刚刚在工作应用中突然看到了协议异常。从今天开始,所有网络呼叫都在我们的应用程序中失败并变得空了,但是API正在使用Web浏览器。
我更新了Okhttp&GSON的依赖性在我的应用程序中,但仍然遇到相同的错误。我的服务器名称是nginx。
我还检查了凌空中的同一URL,并成功地从服务器中收到了响应,但是我使用了相同的URL并进行了翻新编写代码,并且它在那里不起作用(服务器没有响应(
这与Android OKHTTP问题有关还是与服务器相关的问题?
mainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
recyclerview.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerview.setLayoutManager(layoutManager);
tabs_adapter = new Tabs_Adapter(MainActivity.this, topTabList);
recyclerview.setAdapter(tabs_adapter);
new AsyncTask<JSONObject, Object, Tabs_Response>() {
@Override
protected Tabs_Response doInBackground(JSONObject... jsonObjects) {
try {
String url = "https://api.bargaincry.com/apurl/deal/get_ApiCategory/41187";
Request request = new Request.Builder().url(url).build();
Response response = new OkHttpClient().newCall(request).execute();
strResponse = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
Tabs_Response tabs_response = new Gson().fromJson(strResponse,Tabs_Response.class);
return tabs_response;
}
@Override
protected void onPostExecute(Tabs_Response tabs_response) {
if (tabs_response != null && tabs_response.getTopTabs() != null && tabs_response.getTopTabs().size() > 0) {
topTabList = tabs_response.getTopTabs();
tabs_adapter = new Tabs_Adapter(MainActivity.this, topTabList);
recyclerview.setAdapter(tabs_adapter);
}
}
}.execute();
}
tap_response.java:
public class Tabs_Response {
@SerializedName("status")
@Expose
private String status;
@SerializedName("top_tabs")
@Expose
private List<TopTab> topTabs = null;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<TopTab> getTopTabs() {
return topTabs;
}
public void setTopTabs(List<TopTab> topTabs) {
this.topTabs = topTabs;
}
}
toptab.java:
public class TopTab {
@SerializedName("category_id")
@Expose
private String categoryId;
@SerializedName("category_name")
@Expose
private String categoryName;
public String getCategoryId() {
return categoryId;
}
public void setCategoryId(String categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
tabs_adapter.java:
public class Tabs_Adapter extends RecyclerView.Adapter {
private Context context;
private List<TopTab> tabList;
private TopTab topTab;
public Tabs_Adapter(Context context, List<TopTab> tabList){
this.context = context;
this.tabList = tabList;
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public TopTab topTab;
public MyViewHolder(View itemView) {
super(itemView);
textView = (TextView)itemView.findViewById(R.id.tabtext_data);
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.list_items,parent,false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
topTab = tabList.get(position);
((MyViewHolder)holder).textView.setText(topTab.getCategoryName());
((MyViewHolder)holder).topTab =topTab;
}
@Override
public int getItemCount() {
if (tabList != null){
return tabList.size();
}else {
return 0;
}
}
logcat:
java.net.protocolexception:预期':状态'标题不存在 02-14 11:38:52.468 12631-19978/com.example.srikanth.toptabs_activity w/system.err:在 com.squareup.okhttp.internal.http.framedtransport.readnamevalueblock(framedtransport.java:197( 02-14 11:38:52.468 12631-19978/com.example.srikanth.toptabs_activity w/system.err:在 com.squareup.okhttp.internal.http.framedtransport.readresponseheaders(framedtransport.java:104( 02-14 11:38:52.468 12631-19978/com.example.srikanth.toptabs_activity w/system.err:在 com.squareup.okhttp.internal.http.httpengine.readnetworkresponse(httpengine.java:906( 02-14 11:38:52.468 12631-19978/com.example.srikanth.toptabs_activity w/system.err:在 com.squareup.okhttp.internal.http.httpengine.access $ 300(httpengine.java:92( 02-14 11:38:52.468 12631-19978/com.example.srikanth.toptabs_activity w/system.err:在 com.squareup.okhttp.internal.http.httpengine $ networkInterceptorchain.proceed(httpengine.java:891(
更新retrofit和okhttp3库中此版本。
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
固定在OKHTTP的当前版本中。请升级。