我有以下代码来执行 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);
永远不会被调用。有人知道我哪里出错了吗?