我刚刚在一个正常工作的应用中看到了协议异常。从今天开始,所有网络调用都在我们的应用程序中失败并变空,但API正在使用Web浏览器。
我更新了okHttp&我的应用程序Gradle中的Gson依赖项但仍然得到相同的错误。我的服务器名称是nginx。
我还在排球中检查了相同的网址并成功获得了服务器的响应,但是我使用了相同的网址并在Retrofit中编写了代码并且它没有在那里工作(没有来自服务器的响应)
这是否与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:预期':status'标头不存在 02-14 11:38:52.468 12631-19978 / com.example.srikanth.toptabs_activity W / System.err:at 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:at 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:at 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:at 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:at com.squareup.okhttp.internal.http.HttpEngine $ NetworkInterceptorChain.proceed(HttpEngine.java:891)
答案 0 :(得分:1)
已在当前版本的OkHttp中修复。请升级。
答案 1 :(得分:1)
将Retrofit和Okhttp3库更新到此版本。
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.retrofit2:retrofit:2.3.0'