我正在使用json将数据发布到服务器。工作正常。但是当我使用progressbar显示进度时。它不起作用。我的进度栏根本没有显示,但是仍然有数据发布并得到响应
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leave);
progressBar = findViewById(R.id.progressBarLeave);
submit_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE);
sendData()
}
}
}
public void sendData(){
try {
String api_url = "https://url/";
String json_request = "{\"test\":\""+1+"\"}";
Log.e("driver_loc", location_request);
JSONObject response_data = call_api(api_url, json_request);
Log.e("response", String.valueOf(response_data));
if(response_data.getString("status").equals("1")) {
Toast.makeText(Leave.this, "Success", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(),Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
}
public JSONObject call_api(String api_url, String request_data) {
try {
URL url = new URL(api_url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(request_data);
writer.flush();
writer.close();
os.close();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
String response = "";
while ((line = bufferedReader.readLine()) != null) {
response += line;
}
Log.d("API response", response);
JSONObject response_data = new JSONObject(response);
return response_data;
} catch (Exception e) {
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}
return null;
}
发布和获取响应正常,但进度条未显示。我想显示进度条,直到我收到服务器的响应。
答案 0 :(得分:1)
尝试将sendData包装在可运行的任务中。可能只是因为冻结用户界面而没有显示
image = Image.open(file).convert('RGB')
image = Image.open(file)
image = image.convert('P')
pp = image.getpalette()
pp[0] = 255
pp[1] = 0
pp[2] = 255
image.putpalette(pp)
image = Image.open('feather.png')
result = image.quantize(colors=256, method=2)