当Json结果= null时,应用程序停止了?

时间:2016-05-02 18:04:05

标签: android json

我在登录页面上工作,当互联网连接关闭它显示我不正确的电子邮件和密码,但当互联网连接打开,我输入错误的登录电子邮件和密码应用程序停止。我不知道怎么解决这个请帮帮我。

这是我的代码:

  @Override
    protected String doInBackground(String... params) {
        String login_email = params[0];
        String login_password = params[1];

        try {
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String data = URLEncoder.encode("login_email", "UTF-8") + "=" + URLEncoder.encode(login_email, "UTF-8") + "&" +
                    URLEncoder.encode("login_password", "UTF-8") + "=" + URLEncoder.encode(login_password, "UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            // String response = "";
            // JSONString = "";
            StringBuilder stringBuilder = new StringBuilder();
            while ((JSONString = bufferedReader.readLine()) != null) {
                // response += JSONString;
                stringBuilder.append(JSONString + "\n");
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return stringBuilder.toString().trim();

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

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        loading.dismiss();
        if (result != null) {
            ParseJson(result);
        }
        else {
            Toast.makeText(Login.this, "Incorrect Email and Password", Toast.LENGTH_LONG).show();
        }


    }

LOGCAT:

05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime: Process: com.example.mubbasher.howdy_sendgreetings, PID: 26814
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java:300)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:841)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:  Caused by: java.lang.NullPointerException
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at libcore.net.UriCodec.encode(UriCodec.java:132)   
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.net.URLEncoder.encode(URLEncoder.java:57)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at com.example.mubbasher.howdy_sendgreetings.NewsFeedFragment$BackgroundSearch.doInBackground(NewsFeedFragment.java:109)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at com.example.mubbasher.howdy_sendgreetings.NewsFeedFragment$BackgroundSearch.doInBackground(NewsFeedFragment.java:89)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:841) 

1 个答案:

答案 0 :(得分:1)

始终在堆栈跟踪中查找“由...引起”

05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:  Caused by: java.lang.NullPointerException
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at libcore.net.UriCodec.encode(UriCodec.java:132)   
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.net.URLEncoder.encode(URLEncoder.java:57)

如果查看UriCodec.encode的来源,您会看到无法传递空字符串,因为内部代码调用s.length()而不检查s

public final String encode(String s, Charset charset) {
    // Guess a bit larger for encoded form
    StringBuilder builder = new StringBuilder(s.length() + 16);
    appendEncoded(builder, s, charset, false);
    return builder.toString();
}

源:

https://android.googlesource.com/platform/libcore/+/fe39951ed36353002263aefef7963a8c8b9c6441/luni/src/main/java/libcore/net/UriCodec.java#130

<强>更新

简短的回答是这样的:你不能将空字符串传递给UrlEncoder.encode,因为这个方法在内部调用UriCodec.encode,它访问String上的length()方法,而不检查它。如果您需要澄清,那么Jimmy是正确的,您需要阅读他发布的链接作为第一条评论。解决方案应该是首先对字符串进行null检查,或者创建一个包装器方法,如果输入为null则返回空字符串;如果它不为null或为空,则调用UrlEncoder.encode