进度条在调试模式下自动隐藏,但不能简单地运行

时间:2017-08-18 15:37:22

标签: android

在运行我的Android应用程序时我遇到了意外的问题。我使用进度条AsyncTask调用rest API。 我在调试模式下以某种方式正确加载并正确隐藏。 在没有调试模式的情况下运行我的应用程序时,进度正确加载但不会自 以下是我的代码

' MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    TextView tv1;
    ProgressDialog pd;
    Button Btngetdata;
    String myJSON;

    //URL to get JSON Array
//    protected static String url = "http://192.168.1.104:856/api/Quotes/GetQuote/EN";


    JSONArray user = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        tv1 = (TextView) findViewById(R.id.textviewquotes);
        Btngetdata = (Button) findViewById(R.id.getdata);
        Btngetdata.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                getData();
              //   new JSONParse().execute("http://192.168.1.104:856/api/Quotes/GetQuote/EN");
                String url = "http://192.168.1.104:856/api/Quotes/GetQuote/EN";
            }
        });
    }

    public void getData() {
        Log.w("Tag", "Get Call");

        JSONParse g = new JSONParse();
        g.execute("");
        Log.w("Tag", "Async call end");
    }

    public void showList() {
        String emplist = "";
        try {
            JSONObject jobj = new JSONObject(myJSON);
            String s = jobj.getString("QuoteText");
            tv1.setText(s);
        } catch (Exception e) {

        }

    }

    private class JSONParse extends AsyncTask<String, Void, String> {
        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

          /*  pd = new ProgressDialog(MainActivity.this);
            pd.setMessage("Please wait");
            pd.setCancelable(false);
            pd.show();*/


            // pd.dismiss();
        }

        @Override
        protected String doInBackground(String... params) {
            String xeno = params[0];
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("QuoteID", xeno));
            InputStream inputStream = null;
            String result = null;
            try {
                android.os.Debug.waitForDebugger();
                DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());


                HttpGet httpPost = new HttpGet("http://192.168.1.104:856/api/Quotes/GetQuote/EN");
                //     httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httpPost);
                HttpEntity entity = response.getEntity();
                inputStream = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                Log.i("Tag", sb.toString());
                result = sb.toString();

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
                return "Error from remote server";
            } finally {
                try {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (Exception ex) {

                }
            }
            return result;
        }

        @Override
        protected void onPostExecute(String s) {
             /*   if (pd.isShowing()) {
                    pd.dismiss();  */

            myJSON = s;
            showList();
        }
    }
}'

感谢您的帮助:) 我是Android中的新蜜蜂。

0 个答案:

没有答案