Java 如何在Android中正确执行HttpPost on-PageFinished

Java 如何在Android中正确执行HttpPost on-PageFinished,java,php,android,http-post,Java,Php,Android,Http Post,在我的应用程序中,我有一个网络视图。如果加载此WebView,我想执行HttpPost从脚本中获取变量。但是,我不断收到异常错误,这些错误告诉我需要在AsyncTask中执行HttpPost。我不知道如何做到这一点,因为我在Android开发方面已经足够好了 这是我写的HttpPost HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new; HttpPost("myscript.php");

在我的应用程序中,我有一个网络视图。如果加载此WebView,我想执行
HttpPost
从脚本中获取变量。但是,我不断收到异常错误,这些错误告诉我需要在
AsyncTask
中执行
HttpPost
。我不知道如何做到这一点,因为我在Android开发方面已经足够好了

这是我写的
HttpPost

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new;   
HttpPost("myscript.php");

    try {
       // Add your data
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
       nameValuePairs.add(new BasicNameValuePair("website", "google.com"));
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

       // Execute HTTP Post Request
       HttpResponse response = httpclient.execute(httppost);

       // writing response to log
       Log.d("Http Response:", response.toString());

     } catch (ClientProtocolException e) {
       // TODO Auto-generated catch block
     } catch (IOException e) {
       // TODO Auto-generated catch block
     }
这是php文件:

<?php

        $website = $_POST['website'];

    $conn = mysql_connect("localhost", "username", "password") or die("err");
    $db = mysql_select_db('database') or die("err");

    $sql = "SELECT color FROM colors WHERE website='$website'";
    $result = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_array($result);
    $color = $row['color'];

    print "$color";

?>

android中的所有网络操作都需要在单独的线程中执行,AsyncTask是一个类,允许您以优雅的方式执行线程(无痛线程)。以下是asynctask的一个示例:

public class SendRequestAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        //runs in ui thread 
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        //perform network operations here it is the background thread
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        //runs in ui thread you can update the layout here
    }
}

谢谢,但是我该如何在我的文件中实现这一点呢?我是否应该将其作为一个单独的类文件添加?您可以将其作为一个内部类,将您在HttpClient上发布的所有代码复制到doInbackground。这样做将使您能够访问外部类的所有类变量。出于这个原因,我通常将其设置为innerclass。您的代码工作起来很有魅力,但我在php文件中做了一些错误的事情,因为它无法正确读取php返回的内容。(php发送01、02、03、04、05或06)我得到的是
Http响应:﹕ [06-27 14:27:06.628 6574:6574 I/NSApplication]
org.apache.http.message。BasicHttpResponse@41d0bac8
作为响应。HttpPost HttpPost=new;HttpPost(“myscript.php”);您的url在这行中可能是错误的
public class SendRequestAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        //runs in ui thread 
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        //perform network operations here it is the background thread
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        //runs in ui thread you can update the layout here
    }
}
new SendRequestAsyncTask().execute();