doucheng7808 2016-01-13 21:54
浏览 68

通过localhost服务器连接android与php文件

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)
  • 写回答

1条回答 默认 最新

  • dongpian2559 2016-01-14 02:00
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码