i was trying to simply check login from android device to php file using xampp server. following is my aysntask code. At post_data URLEncoder it is showing error of null pointer exception. I am unable to solve it.:
Following is my Code.
public class BackgroundWorker extends AsyncTask<String, Void, String> {
String post_data;
Context context;
AlertDialog alertDialog;
public BackgroundWorker(Context ctx) {
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://10.2.67.174/login/login.php";
if (type.equals("login")) {
try {
String user_name = params[1];
String pass_word = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name, "UTF-8")+"&"
+URLEncoder.encode("pass_word","UTF-8")+"="+URLEncoder.encode(pass_word, "UTF-8");
// String post_data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&"
// + URLEncoder.encode("pass_word", "UTF-8") + "=" + URLEncoder.encode(pass_word, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while((line = bufferedReader.readLine())!=null){
result+=line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
Toast.makeText(context,"Success",Toast.LENGTH_LONG).show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
And it is showing error like this.:
01-13 16:45:02.261 2129-2129/com.example.shivam.phpconnect E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache
01-13 16:45:02.261 2129-2129/com.example.shivam.phpconnect E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384
01-13 16:45:02.273 2129-2129/com.example.shivam.phpconnect E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
01-13 16:45:02.273 2129-2129/com.example.shivam.phpconnect E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384
01-13 16:45:10.725 2129-2152/com.example.shivam.phpconnect E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: com.example.shivam.phpconnect, PID: 2129
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at libcore.net.UriCodec.encode(UriCodec.java:132)
at java.net.URLEncoder.encode(URLEncoder.java:57)
at com.example.shivam.phpconnect.BackgroundWorker.doInBackground(BackgroundWorker.java:57)
at com.example.shivam.phpconnect.BackgroundWorker.doInBackground(BackgroundWorker.java:24)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
From the exception, it appears that the URLencoder.encode
method is throwing the exception. Double check that your user_name
and pass_word
Strings, set by params[1]
and params[2]
, are not null.
Not sure if it will help, but give it a shot.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.