dongpouda6700 2014-08-16 19:07 采纳率: 100%
浏览 28
已采纳

Android与PHP交谈

I am new to Android and am currently trying to connect Android to a database through PHP. I can easily connect the PHP script to my database but I cannot get Android to talk to the PHP script. I have tried multiple examples - http://www.androidaspect.com/2013/05/how-to-connect-android-with-php.html - but cannot seem to get it to work. If I could get Android to simply receive a message saying 'hello' from the PHP script, I would easily be able to finish my work but I cannot. I have been told to do things such as use ASync but quite frankly that just makes this so much more complicated. Any help would be appreciated in getting my Android side to talk to my PHP side. Thank you very much in advance.

I've deleted the actual code that I've tried but one tutorial is linked above and the other had me try this -

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("rec","mar"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/select.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost); 
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader
            (new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
   sb.append(line + "
");
}
is.close();
result = sb.toString();
JSONObject json_data = new JSONObject(result);
name=(json_data.getString("name"));
Toast.makeText(getBaseContext(), "Name : "+name,
        Toast.LENGTH_SHORT).show();
  • 写回答

2条回答 默认 最新

  • dpe77294 2014-08-16 20:33
    关注

    Use below class as inner class and change url and parameters :

    class PlaceOrder extends AsyncTask<Void, Void, Void> {
    
            @Override
            protected Void doInBackground(Void... params) {
    
                // TODO Auto-generated method stub
    
                try {
    
                    HttpClient httpClient = new DefaultHttpClient();
    
                    HttpPost httpPst = new HttpPost(
    
                    "yout_url");
    
                    ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>(
    
                    2);
    
                    parameters.add(new BasicNameValuePair("username", "apple"));
    
                    parameters.add(new BasicNameValuePair("pw", "apple"));
    
                    parameters.add(new BasicNameValuePair("email",
                            "apple@gmail.com"));
    
                    parameters.add(new BasicNameValuePair("name", "apple"));
    
                    httpPst.setEntity(new UrlEncodedFormEntity(parameters));
    
                    HttpResponse httpRes = httpClient.execute(httpPst);
    
                    String str = convertStreamToString(
                            httpRes.getEntity().getContent()).toString();
    
                    Log.i("mlog", "outfromurl" + str);
    
                } catch (UnsupportedEncodingException e) {
    
                    // TODO Auto-generated catch block
    
                    e.printStackTrace();
    
                } catch (ClientProtocolException e) {
    
                    // TODO Auto-generated catch block
    
                    e.printStackTrace();
    
                } catch (IOException e) {
    
                    // TODO Auto-generated catch block
    
                    e.printStackTrace();
    
                }
    
                return null;
    
            }
    
        }
    
        public static String convertStreamToString(InputStream is) {
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    
            StringBuilder sb = new StringBuilder();
    
            String line = null;
    
            try {
    
                while ((line = reader.readLine()) != null) {
    
                    sb.append(line + "
    ");
    
                }
    
            } catch (Exception e) {
    
                e.printStackTrace();
    
            } finally {
    
                try {
    
                    is.close();
    
                } catch (IOException e) {
    
                    e.printStackTrace();
    
                }
    
            }
    
            return sb.toString();
    
        }
    

    use http://json.parser.online.fr/ for validating json

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据