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

时间:2016-01-13 21:54:03

标签: php android xampp

我试图使用xampp服务器简单地检查从android设备登录到php文件。以下是我的aysntask代码。在post_data URLEncoder中,它显示空指针异常的错误。我无法解决它:

以下是我的代码。

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);
    }
}

它显示的是这样的错误。:

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 个答案:

答案 0 :(得分:0)

从例外情况看,URLencoder.encode方法似乎抛出了异常。请仔细检查由user_namepass_word设置的params[1]params[2]字符串是否为空。

不确定它是否会有所帮助,但请试一试。