Android和PHP创建API [英] Android and PHP Creating an API

查看:178
本文介绍了Android和PHP创建API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个PHP API为我的Andr​​oid应用程序进行交互,我的问题是,POST数据似乎从来就没贴,我永远无法检索给出的URL响应正文(HTML / TXT) 。

I'm trying to set up a PHP API for my Android application to interact with, my problem is that the post data never seems to get posted and I can never retrieve the response body (HTML/TXT) from the URL given.

我的code

Thread thread = new Thread(new Runnable(){
        @Override
        public void run() {
            try {
                HttpClient client = new DefaultHttpClient();
                HttpPost clientpost = new HttpPost("http://192.168.1.129/updateMain.php");

                try {
                    List<NameValuePair> params = new ArrayList<NameValuePair>(2);

                    params.add(new BasicNameValuePair("_id", "1"));
                    params.add(new BasicNameValuePair("job_name", "Test"));

                    clientpost.setEntity(new UrlEncodedFormEntity(params));

                    HttpResponse response = client.execute(clientpost);

                    String responseBody = EntityUtils.toString(response.getEntity());

                } catch(Exception e)
                {
                    Log.e("Error", e.toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    thread.start();

如果我可以发布数据,那么我将能够张贴JSON到服务器,从服务器获取JSON,从而克服我目前所面对的最大的障碍。

If I can post data then I will be able to post JSON to the server and retrieve JSON from the server, thus overcoming the biggest hurdle I have at the moment.

任何帮助非常AP preciated。

Any help greatly appreciated.

推荐答案

这一个是为我工作。请你照顾答异常错误处理。 Key121变量是你的PHP文件的URL。

This one is working for me. Please do take care of exceptions ans error handling. Key121 variable is your php file url.

  class callServiceTask extends AsyncTask<Void, Void, Void>
{
    @Override
    protected Void doInBackground(Void... params) {
    loginCheck();
    return null;
    }
} 
public void loginCheck()
{
InputStream is = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("aa","11"));   
nameValuePairs.add(new BasicNameValuePair("bb","22"));

    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(KEY_121);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    }catch(Exception e)
    {
        Log.e("log_tag", "Error:"+e.toString());
    }
//convert response to string
    try{
    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
    sb.append(line + "\n");
    }
    is.close();
    result=sb.toString();
    Log.e("log_tag", "----------------"+result);
    }catch(Exception e){
    Log.e("log_tag", "Error converting result "+e.toString());
    }
}

您需要使用流和缓冲读者分析响应。请检查并让我们知道。

You need to use stream and buffered reader for analysing response. Do check and let us know.

这篇关于Android和PHP创建API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆