java.lang.RuntimeException:执行doInBackground()URI时发生错误

时间:2016-02-06 16:50:21

标签: java android

我是Android编程新手 我正在尝试为应用程序编写代码 我正在尝试使用URI 但它一直给出以下错误 寻求帮助

java文件

package com.example.ayushspc.digilocker;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class GetRegDetails extends AppCompatActivity {
    Register register = new Register();
    EditText ET_MOBILE, ET_EMAIL;
    String json_string;
    JSONArray jsonArray;
    JSONObject jsonObject;
    String name, branch, email, mobile, enroll, OTP;
    OTPGenerator otpGenerator;
    Sendsms sendsms;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        otpGenerator = new OTPGenerator();
        sendsms = new Sendsms();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_reg_details);

        enroll = register.enroll;
        json_string = getIntent().getExtras().getString("jsonresult");

        try {
            jsonObject = new JSONObject(json_string);
            jsonArray = jsonObject.getJSONArray("server_response");
            int count = 0;
            while (count < jsonArray.length()) {
                JSONObject JO = jsonArray.getJSONObject(count);
                name = JO.getString("name");
                branch = JO.getString("branch");
                count++;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        TextView tv_name = (TextView) findViewById(R.id.ver_name);
        TextView tv_branch = (TextView) findViewById(R.id.ver_branch);
        tv_name.setText(name);
        tv_name.setVisibility(View.VISIBLE);
        tv_branch.setText(branch);
        tv_branch.setVisibility(View.VISIBLE);

    }

    public void verifyDetails(View view) {
        ET_MOBILE = (EditText) findViewById(R.id.ver_mobile);
        ET_EMAIL = (EditText) findViewById(R.id.ver_email);
        mobile = ET_MOBILE.getText().toString();
        email = ET_EMAIL.getText().toString();
        OTP = otpGenerator.generator();
        String number = mobile.toString();
        // sendsms.message(number, OTP);//add enrollment number to message;pass mesage as an argument
        Toast.makeText(getApplicationContext(), mobile + " " + OTP, Toast.LENGTH_SHORT).show();
        new BackgroundTask().execute(enroll, mobile, email, OTP);
        Toast.makeText(getApplicationContext(), OTP, Toast.LENGTH_LONG).show();

    }

    class BackgroundTask extends AsyncTask<String, Void, String> {
        String otp_url = "http://www.digiloc.esy.es/otp_verification.php";

        @Override
        protected String doInBackground(String... params) {
            String enrollment, mobile, email, otp;
            enrollment = params[0];
            mobile = params[1];
            email = params[2];
            otp = params[3];

            try {
                URL url = new URL(otp_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String data = URLEncoder.encode("enroll", "UTF-8") + "=" + URLEncoder.encode(enrollment, "UTF-8") + "&" +
                        URLEncoder.encode("mobile", "UTF-8") + "=" + URLEncoder.encode(mobile, "UTF-8") + "&" +
                        URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&" +
                        URLEncoder.encode("otp", "UTF-8") + "=" + URLEncoder.encode(otp, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }
    }
}

日志文件

02-06 22:12:05.140  11850-12852/com.example.ayushspc.digilocker E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #4
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:864)
     Caused by: java.lang.NullPointerException
            at libcore.net.UriCodec.encode(UriCodec.java:132)
            at java.net.URLEncoder.encode(URLEncoder.java:57)
            at com.example.ayushspc.digilocker.GetRegDetails$BackgroundTask.doInBackground(GetRegDetails.java:102)
            at com.example.ayushspc.digilocker.GetRegDetails$BackgroundTask.doInBackground(GetRegDetails.java:84)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:864)
02-06 22:12:05.400  11850-11850/com.example.ayushspc.digilocker E/copybit﹕ Error opening frame buffer errno=13 (Permission denied)

有人可能会指出错误的原因  还有一种解决方法

1 个答案:

答案 0 :(得分:0)

enroll = register.enroll;

register.enroll返回null。显示Register课程或尝试自己查找问题。