【问题标题】:get response from php and save in a file android从 php 获取响应并保存在 android 文件中
【发布时间】:2014-05-20 12:08:22
【问题描述】:

我正在尝试从 android 向 PHP 文件发出请求。之后,我需要将 PHP 生成的响应(单个代码)保存到一个文件中,这样我下次就可以读取它而无需再次发出请求。对于请求,我创建了一个新方法,内容如下:

> private String deviceIdHttpGetter(){
>           HttpHandler handler =   new HttpHandler();
>           String code=handler.post("http://www.cafegourmet.es/newUser.php");
>           Toast.makeText(getApplicationContext(), "your code is " + code, Toast.LENGTH_SHORT).show();
>               return code;
>               
>           }

我还修改了 HttpHandler post() 方法,这样我就可以获取代码并返回它,所以现在它看起来像这样:

>   public String post(String postURL){
>           try {
>               HttpClient client = new DefaultHttpClient();
>               HttpPost post = new HttpPost(postURL);
>               HttpResponse response = client.execute(post);
>               HttpEntity ent = response.getEntity();
>               String text = EntityUtils.toString(ent);
>               return text;
>           }
>     
>           catch(Exception e) { return e.getMessage();} 
>     }

现在的 PHP 内容如下:

<?php $code = 12345; echo $code; ?>

嗯...问题是,我不知道为什么,但我总是看到 Toast 好像没有收到任何代码(“你的代码是”),但是当我从资源管理器访问这个 PHP 时,我总是得到我需要的新代码,因此我无法将数据保存到文件中。

有什么建议吗?在此先感谢大家

【问题讨论】:

  • 本地服务器上是否有 php 文件?
  • 在您的 post 函数中,尝试在 catch 块中返回空字符串以外的其他内容(如“error”)。这样,您就可以区分 try 块中的错误和 php 页面中的空白响应。
  • Did,它正在返回 "error" ,当时 Toast 显示“您的代码错误”。不,php 在远程服务器上。将正确的 url 添加到代码中,如果你想检查,它是一个自动递增的 int 它现在返回的内容
  • 好的,让它返回 e.getMessage() 而不是 "error" 并发布它给你的错误消息
  • 经过一些(愚蠢的)编辑后,代码不再抛出异常,而是不返回代码,所以我猜现在错误在 Try 的代码中。现在更新代码,我所做的只是在 try 中包含 String 声明,并在我不再使用它时抑制布尔值。我也一直在检查,并且由于 PHP 返回的代码是一个自增的 int,就好像它甚至没有执行请求一样。

标签: php android file save response


【解决方案1】:

这是解决问题的代码,希望对某人有所帮助:

首先,在主Activity中调用async的代码如下:

private String deviceIdHttpGetter(){
        try
        {
        AsyncHttpDeviceId asyncId = new AsyncHttpDeviceId();
        return asyncId.execute("http://www.cafegourmet.es/newUser.php").get();
        }
        catch(Exception e){
            return e.toString();
        }
    }
}

Async 最终如下所示:

public class AsyncHttpDeviceId extends AsyncTask<String, Integer, String> {
    String code="";

     public AsyncHttpDeviceId () {
        }

        /**
         * background
         */
     protected String doInBackground(String... posturl ) {
               HttpClient httpclient = new DefaultHttpClient();
               HttpResponse response;
               String responseString = null;
               try {
                   response = httpclient.execute(new HttpGet(posturl[0]));
                   StatusLine statusLine = response.getStatusLine();
                   if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                       ByteArrayOutputStream out = new ByteArrayOutputStream();
                       response.getEntity().writeTo(out);
                       responseString = out.toString();
                       out.close();
                   } else{
                       //Closes the connection.
                       response.getEntity().getContent().close();
                       throw new IOException(statusLine.getReasonPhrase());
                   }
               } catch (ClientProtocolException e) {
                   return e.toString();
               } catch (IOException e) {
                   return e.getMessage();
               }
               return responseString;
      }
      protected void onPostExecute(String result) {
            code=result;
      }
}

希望有一天这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多