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

Posted

技术标签:

【中文标题】通过 localhost 服务器连接 android 和 php 文件【英文标题】:connecting android with php file over localhost server 【发布时间】:2016-04-19 02:00:13 【问题描述】:

我试图使用 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)

【问题讨论】:

'BackgroundWorker.java:57'。那是哪一行代码? 不要对您的帖子数据进行 url 编码。 请说明您如何调用异步任务。 【参考方案1】:

从异常看来,URLencoder.encode 方法正在抛出异常。仔细检查由params[1]params[2] 设置的user_namepass_word 字符串不为空。

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

【讨论】:

以上是关于通过 localhost 服务器连接 android 和 php 文件的主要内容,如果未能解决你的问题,请参考以下文章

oracle可以通过localhost连接,无法通过ip地址连接解决方法,oracle远程连接配置

为什么用localhost可以连接,本机ip无法连接

为 localhost 和 LAN 连接设置 SSL

如何在 Android Studio 中连接或链接到 localhost 服务器地址

连接putty的xampp localhost密码是啥?

本地oracle可以通过localhost连接,无法通过ip地址连接解决方法,oracle远程连接配置