dqvzfp6468 2014-06-26 06:44
浏览 34

从哪里开始在Android中的Web服务?

Some people vote down after seeing these kinds of question. So if anyone votedown please tell the reason so that I may improve.

I am learning Android and I started studying webservices.

I used this php code

<?php 
echo "hello abhi how are you";
?>

and this code in the Activity class of android application

public class MainActivity extends Activity {

        BufferedReader in = null;
        TextView tv1;
        HttpClient httpclient;
        HttpPost httppost;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                tv1 = (TextView) findViewById(R.id.tv1);
                httpclient = new DefaultHttpClient();
                httppost = new HttpPost("http://10.0.2.2/devilabhi/a.php");
                new getresult().execute();


        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
        }

        public class getresult extends AsyncTask<Void, Void, Void>{


                String result = null;
                @Override
                protected Void doInBackground(Void... arg0) {
                        // TODO Auto-generated method stub
                        try {
                                HttpResponse response = httpclient.execute(httppost);
                                result = EntityUtils.toString(response.getEntity());


                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                Log.e("Exception", e.toString());
                        }
                        return null;
                }

                @Override
                protected void onPostExecute(Void result1) {
                        // TODO Auto-generated method stub
                        super.onPostExecute(result1);
                        tv1.setText(result);
                        Log.i("My Response :: ", result);
                }

        }

}

and I got output as

enter image description here

Now next step I want to learn is how to save name and email or anything else in database on server and how to retrieve that or I must study something else first so that I may understand this thing easily I searched regarding this and I got the link but there is no php code so I may not try this code

Is there any website, link or any example for us beginners to study and learn webservices.

Your suggestions and answers and very valuable for me and beginners like me. Thanks in Advance

  • 写回答

1条回答 默认 最新

  • dongya2029 2014-06-26 06:50
    关注

    The reason these get frequently downvoted is that they frequently get asked without showing the asker has put any effort into researching how to do things. You've at least shown some, so I'll give you some next steps.

    1)Sending parameters. You're going to need to choose whether to use an HTTP GET or HTTP POST request. In a GET, the parameters are embedded in the url- http://example.com?param1=val1&param2=val2 for example. In a POST, they're embedded in the request. There's reasons to use each type, but you can google that later.

    2)On the PHP side, you can read the values using $_GET["param1"] or $_POST["param1"] depending on the way you sent them.

    3)You then have to connect to the database and write SQL queries to INSERT the data. You're best off googling for a tutorial on using your specific db and PHP here.

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号