【问题标题】:How to get a reply from PHP for a POST request如何从 PHP 获得对 POST 请求的回复
【发布时间】:2017-09-29 19:59:12
【问题描述】:

我已经为来自 android 应用程序的 POST 请求编写了代码,该应用程序将在服务器上运行一个 php 文件。

    String url = "http://****/****/Servlets.php";

    RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
    StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>(){
        @Override
        public void onResponse(String response){
            //code when server responds
        }
    },new Response.ErrorListener(){
        @Override
        public void onErrorResponse(VolleyError error){
            //Code if error is there
            int test2 = 1;
        }
    }){
        protected Map<String, String> getParams() {
            Map<String, String> MyData = new HashMap<String, String>();
            MyData.put("key",st);
            return MyData;
        }
    };
    MyRequestQueue.add(MyStringRequest);

Php 文件的代码正在调用一个 java 文件并获取一些字符串值。如何将该字符串值返回到可以传递给 onresponse() 参数的 android 设备?响应是否等待或者我们如何处理它?

请提出建议。

【问题讨论】:

  • 这里为什么要用双斜线。 ****//****//?
  • 有没有办法捕获异常?如果你能建议我一种发布错误的方法,那就太谦虚了
  • 有一个public void onErrorResponse(VolleyError error){ 所以在那里打印 volleyerror 看看响应是什么
  • 感谢@Kris,该链接帮助我改变了方法并提出了更快的解决方案。

标签: java php android post android-volley


【解决方案1】:

尝试使用此代码而不是您的方式:

public String performPostCall(String requestURL, HashMap<String, String> postDataParams) {
    URL url;
    String response = "";
    try {
        url = new URL(requestURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);


        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));

        writer.flush();
        writer.close();
        os.close();
        int responseCode = conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            String line;
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = br.readLine()) != null) {
                response += line;
            }
        } else {
            response = "";

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return response;
}

然后你可以通过编写代码得到结果:

 HashMap<String, String> params = new HashMap<String, String>();
    postDataparams.put("key", value);
 String response = performPostCall("url", postDataParams);

这样您就可以从您的服务器获得响应。在服务器端,您只需键入 echo 需要发送到设备的任何内容。没有其他事情需要做。此异步线程将一直等待,直到它从服务器获得响应。

希望这会有所帮助。

【讨论】:

  • 只需在我的 Php 端回显“Hello”就可以了?还是我必须写更多的东西来处理响应?
  • 回显“响应”;将以响应的形式发回字符串。
  • 完美运行,简单易行。谢谢学习者。
猜你喜欢
  • 2013-04-02
  • 1970-01-01
  • 1970-01-01
  • 2021-09-09
  • 2013-06-14
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
相关资源
最近更新 更多