0

我有以下代码来执行 PHP 脚本:

public class CallPHPScript extends AsyncTask<ScriptNameAndParameters, Void, String> {
    private Reply responder;

    public interface Reply {
        public void serverReply(String reply);
    }

    public CallPHPScript(Reply r) {
        responder = r;
    }

    @Override 
    protected String doInBackground(ScriptNameAndParameters... arg) {
        List<NameValuePair> params = arg[0].getParameters();
        String scriptName = arg[0].getScriptName();
        String json = MainActivity.makeCall(scriptName, params);
        return json;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Log.d("SERVER REPLY", "Server Reply: " + result);
        responder.serverReply(result);
    }

}

public static String makeCall(String scriptName, List<NameValuePair> params) {
    String address = SERVER_ADDRESS + scriptName + ".php";

    Log.d("Main Activity", "Making call to server with: " + address);

    HttpPost httpPost = new HttpPost(address);
    HttpClient httpClient = new DefaultHttpClient();
    StringBuilder total = new StringBuilder();
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        // Execute HTTP Post Request
        HttpResponse response = httpClient.execute(httpPost);
        InputStream is = response.getEntity().getContent();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line = "";
        // Read response until the end
        while ((line = rd.readLine()) != null) {
            total.append(line);
        }

        // Return full string
        Log.d("CALLPHPSCRIPT", total.toString());

    } catch (Exception e) {
        e.printStackTrace();
    }
    return total.toString();
}

虽然它似乎工作正常,但我能看到的唯一输出是

        // Return full string
        Log.d("CALLPHPSCRIPT", total.toString())

这条线(因此是我的回调)

    Log.d("SERVER REPLY", "Server Reply: " + result);

永远不会被调用。有人知道我哪里出错了吗?

4

3 回答 3

1

既然你解决了问题,你就违反了AsyncTask#3的线程规则

这是使课程正常工作的线程规则

  1. 该类AsyncTask必须在 UI 线程上加载。这是从 开始自动完成的JELLY_BEAN

  2. 任务实例必须在 UI 线程上创建。必须在 UI 线程上调用 execute(Params...)。

  3. 不要手动调用onPreExecute(), onPostExecute(Result), doInBackground(Params...), 。onProgressUpdate(Progress...)

  4. 该任务只能执行一次(如果尝试第二次执行将引发异常。)

于 2013-05-25T15:04:35.460 回答
0

才意识到我应该打电话

    CallPHPScript callPHP = new CallPHPScript(this);
    callPHP.execute(new ScriptNameAndParameters("get_all_profiles",new ArrayList<NameValuePair>()));

代替

    CallPHPScript callPHP = new CallPHPScript(this);
    callPHP.doInBackground(new ScriptNameAndParameters("get_all_profiles",new ArrayList<NameValuePair>()));

嗬!

于 2013-05-25T14:50:45.610 回答
0

(/)

记得优雅地关闭连接管理器和所有输入流

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }

无需调用 super in

super.onPostExecute(result);
于 2013-05-25T15:03:25.497 回答