从登录页面导航到下一页时,会出现空白屏幕

时间:2017-02-06 05:42:58

标签: android json android-asynctask httpurlconnection

我已经集成了一个Web服务,在集成之后进入下一页。 但问题是当从登录页面导航到下一页时,它显示空白屏幕,我不明白问题是什么。 请帮助!!

提前致谢。

这是我的代码

public class Mobile_Number_Activity extends Activity {

    private EditText ed_Mobile_Number;
    private Button btnSubmit;
    private TextView Hyperlink;
    private ProgressDialog pDialog;
    private JSONObject json;
    private int success = 0;
    private String path = "xxx";
    private String strMobileNumber = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mobile_number);
        ed_Mobile_Number = (EditText) findViewById(R.id.ed_Mobile_Number);

        btnSubmit = (Button) findViewById(R.id.btn_mobile_submit);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {

                if (!ed_Mobile_Number.getText().toString().equals(""))
                {
                    strMobileNumber = ed_Mobile_Number.getText().toString();

                    new SendPostRequest(getApplicationContext()).execute(strMobileNumber);
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Please Enter Mobile Number", Toast.LENGTH_LONG).show();
                }

            }
        });

    }

这是SendPost Request类扩展Asyntask

public class SendPostRequest extends AsyncTask<String, Void, String> {

    public static ProgressDialog pDialog;
    private String path = "xxxx";
   public String Mobile_Number = "";
    private  Context mContext;


    public SendPostRequest(Context context)
    {

        this.mContext=context;
        pDialog = new ProgressDialog(mContext);
        pDialog.setTitle("Loading...");
        pDialog.setMessage("Please wait...");

    }

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


    }

    @Override
    protected String doInBackground(String... arg0)
    {
        try
        {
            Thread.sleep(40000);
            String m_number=arg0[0];

            URL url = new URL(path); // here is your URL path
            JSONObject postDataParams = new JSONObject();
            postDataParams.put("username", "data");
            postDataParams.put("password", "data");
            postDataParams.put("mobile", m_number);
            System.out.println(m_number);
            Mobile_Number=m_number;
            Log.e("params", postDataParams.toString());
            //conversion of 15 min to miliseconds :900000
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          //  conn.setReadTimeout(1500/* milliseconds */);
            //conn.setConnectTimeout(1500 /* milliseconds */);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);

            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(getPostDataString(postDataParams));
            writer.flush();
            writer.close();
            os.close();

            int responseCode = conn.getResponseCode();


            if (responseCode == HttpsURLConnection.HTTP_OK) {

                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                StringBuffer sb = new StringBuffer("");
                String line = "";

                while ((line = in.readLine()) != null) {

                    sb.append(line);
                    break;
                }

                in.close();
                return sb.toString();

            } else {
                return new String("false : " + responseCode);
            }
        } catch (Exception e) {
            return new String("Exception: " + e.getMessage());
        }

    }

    @Override
    protected void onPostExecute(String result) {
        if (pDialog != null && pDialog.isShowing()) {

            //Toast.makeText(mContext.getApplicationContext(), result,Toast.LENGTH_LONG).show();
            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(result);
                String otp = jsonObject.optString("otp");
                Log.d("otp value is", otp);
                message(otp);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            pDialog.dismiss();
        }
    }
    public String getPostDataString(JSONObject params) throws Exception {

        StringBuilder result = new StringBuilder();
        boolean first = true;

        Iterator<String> itr = params.keys();

        while (itr.hasNext()) {

            String key = itr.next();
            Object value = params.get(key);
            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(key, "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(value.toString(), "UTF-8"));
        }
        return result.toString();
    }


    public void message(String otp)
    {
        Intent myIntent = new Intent(this.mContext,OTP_Code_Activity.class);
        myIntent.putExtra("Mobile Number", Mobile_Number);
        System.out.println(Mobile_Number);
        myIntent.putExtra("OTP Code", otp);
        mContext.startActivity(myIntent);
        pDialog.dismiss();
    }

3 个答案:

答案 0 :(得分:0)

new SendPostRequest(getApplicationContext()).execute(strMobileNumber);

将此更改为活动上下文。

new SendPostRequest(Mobile_Number_Activity.this).execute(strMobileNumber);

答案 1 :(得分:0)

从手机中删除(卸载)该应用,然后重新运行..由于数据库存在一些问题。或者我认为你没有提到清单中的活动

答案 2 :(得分:0)

更改

new SendPostRequest(getApplicationContext()).execute(strMobileNumber);

new SendPostRequest(this).execute(strMobileNumber);

使用应用程序上下文,您无法显示Dialog。您需要使用Activity实例。

更改

SendPostRequest(Context context) 

SendPostRequest(Activity context) 

它可能会有所帮助。检查日志并查看JSON Parse Exception中是否有任何错误。