从php获取响应并保存在android文件中

时间:2014-05-20 12:08:22

标签: php android file save response

我试图从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好像没有收到任何代码(&#34;你的代码是&#34;)但是当我从资源管理器访问此PHP,我总是得到我需要的新代码,因此我无法将数据保存到文件中。

有什么建议吗?在此先感谢所有人

1 个答案:

答案 0 :(得分:0)

这是解决问题的代码,希望它可以帮助某人:

首先,在主活动中调用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;
      }
}

希望有一天能帮到某人。