私はインターネット接続がオフになっているときにログインページに取り組んでいます。間違った電子メールとパスワードが表示されますが、インターネット接続がオンで、間違ったログインメールとパスワードアプリが停止します。私はこれを解決する方法を知らない私を助けてください。Json result = nullのときにアプリケーションが停止しましたか?
これは私のコードです:
@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)
[Null Pointer Exceptionとは何ですか?それを修正するにはどうすればいいですか?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how -do-i-fix-it) – JimmyB
私はこれらのことをすべて勉強しましたが、私の問題はそのjsonの問題とは異なります.. @JimmyBもし誰かを助けることができないなら他人を離れないように説得しなければならない – bebo
いいえ、あなたの問題違いはありません。あなたのコードをデバッグして、*どのフィールドや変数がnullで、例外を発生させるかを教えてください。 – JimmyB