【发布时间】:2023-03-09 22:26:01
【问题描述】:
我正在使用改造进行解析。我不确定发生了什么,但是从服务器获取数据时突然出现空白屏幕。而且它不规则,有时会出现或有时不会出现。而且不仅限于单个页面,我的意思是它发生在我使用服务的任何活动中。并且在按一次或两次后退按钮后,会出现列表。
调试后我发现它继续 onFailure 改造方法并给出了一个
java.net.SocketTimeoutException 有谁知道原因吗?您的善意支持将不胜感激。
这是我的代码:
GridView mLvCategories;
CustomCategoriesAdapter adapter;
private GetCustomCategoriesModel[] mArray;
ImageView viewImage;
private String[] navMenuTitles;
private TypedArray navMenuIcons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_professional_list);
initializeWidgets();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); // load
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);// load icons from
set(navMenuTitles, navMenuIcons);
callToGetAllCategories();
}
private void initializeWidgets() {
mLvCategories=(GridView)findViewById(R.id.grid_categories);
}
private void callToGetAllCategories() {
final ProgressDialog dialog = ProgressDialog.show(ProfessionalListActivity.this, "", "Please wait...");
RestClient.GitApiInterface service = RestClient.getClient();
String url="http://scorpioinfotech.net/demo/api/get_categories";
Call<GetCustomCategoriesModel[]> call = service.hitGetApi(url);
call.enqueue(new Callback<GetCustomCategoriesModel[]>() {
@Override
public void onResponse(Response<GetCustomCategoriesModel[]> response) {
Log.d("MainActivity", "Status Code = " + response.code());
if (response.isSuccess()) {
// request successful (status code 200, 201)
GetCustomCategoriesModel[] result = response.body();
Log.d("MainActivity", "response = " + new Gson().toJson(result));
if (result.length > 0) {
dialog.dismiss();
mArray = result;
adapter = new CustomCategoriesAdapter(ProfessionalListActivity.this, mArray);
mLvCategories.setAdapter(adapter);
mLvCategories.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent mIntent = new Intent(ProfessionalListActivity.this, SubListProActivity.class);
mIntent.putExtra("sub_id", mArray[position].getCategory_id() + "");
startActivity(mIntent);
}
});
}
} else {
// response received but request not successful (like 400,401,403 etc)
//Handle errors
Toast.makeText(ProfessionalListActivity.this, "Something went wrong!", Toast.LENGTH_SHORT).show();
Log.d("ProfessionalListActivity", "Response is not success");
}
}
@Override
public void onFailure(Throwable t) {
dialog.dismiss();
Log.d("ProfessionalListActivity", "On Failure");
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if (LogInActivity.userLoginViaFb) {
AlertDialog.Builder builder = new AlertDialog.Builder(ProfessionalListActivity.this);
builder.setIcon(R.drawable.find_pro);
builder.setTitle("Find Pro").setMessage("Are you sure you want to exit?")
// Setting Icon to Dialog
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
dialog.dismiss();
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.show();
}
else{
super.onBackPressed();
}
}
请帮忙。
编辑:
public class RestClient {
private static GitApiInterface gitApiInterface ;
public static String baseUrl = "http://scorpioinfotech.net/demo" ;
public static GitApiInterface getClient() {
if (gitApiInterface == null) {
OkHttpClient okClient = new OkHttpClient();
okClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
return response;
}
});
Retrofit client = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverter(String.class, new ToStringConverter())
.client(okClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
gitApiInterface = client.create(GitApiInterface.class);
}
return gitApiInterface ;
}
public interface GitApiInterface {
@GET
Call<GetCustomCategoriesModel[]> hitGetApi(@Url String url);
@GET
Call<Lister[]> hitGetIntroApi (@Url String url);
@GET
Call<GetProDetailInfoModel> hitGetDetailinfoApi (@Url String url);
@GET
Call<Reviews[]> hitGetFeedbackApi (@Url String url);
}
}
【问题讨论】:
-
你在使用同步调用吗?
-
大多数情况下,当您启动您的应用程序时,它会发生 bcoz 改造第一次调用需要时间,之后它会顺利......做一件事只需使用进度条,直到您的数据没有更新。
-
我也在使用进度对话框。但它会停止,一段时间后屏幕变黑
-
你的意思是让应用崩溃?
-
没有应用没有崩溃,只是出现黑屏