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 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?