调用Web API并在Android中接收返回值 [英] Calling web API and Receive return value in Android

查看:119
本文介绍了调用Web API并在Android中接收返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了该主题,但没有得到任何有用的信息.

I've googled for that topics and don't get any useful information.

我想在我的android项目中使用Web API,但不知道如何从android或java调用它们

I want to use Web API in my android projects, but don't know how to call them from android or java

我从网站获得了一些WEB API,并希望在我的android项目中使用. 例如,Web API URL : /api/uname2uid/{uname}.

I've got some WEB API from this site and want to use in my android project. For example, a web api URL : /api/uname2uid/{uname}.

用户名转换为用户数字ID

我想显示从该API返回的用户ID TextView中.所以我需要返回值是字符串或数字.请帮我这个.

I want to display the User ID returned from that API into a TextView. So i need the return value as string or number. Please help me this.

编辑
我只想学习如何使用WEB API
为简单起见: 在我的程序中,我有一个TextView,一个EditText和一个Button
当我按下Button时,TextView将通过EditText字段中给出的用户名的相应用户ID 进行更新.一个有效的示例对我理解这一点非常有用.

EDIT
I just want to learn how to use WEB API
For more simplicity: In my program I have a TextView, aEditText and a Button
When i press the Button then TextView will update by corresponding user ID for Username given in EditText field thats all. An working example will be great for me to understanding this.

推荐答案

由于我只想显示API的 User ID 返回,因此下面的解决方案对我来说很有效.

Since I just want to display the the User ID return from the API, below the solution is work for me.

public class MainActivity extends Activity {

    public static TextView txtUserid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

                    Button btnUserid = (Button) findViewById(R.id.btnUserid);
        btnUserid.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new UserNameToId().execute("https://uhunt.onlinejudge.org/api/uname2uid/almaruf");
            }
        });
    }

    protected class UserNameToId extends AsyncTask<String, Void, String> {
        @Override
        public String doInBackground(String... url) {

            try {
                URL Url = new URL(url[0]);
                URLConnection urlConnection = Url.openConnection();
                InputStream inputStream = urlConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String str = "";
                str = bufferedReader.readLine();
                return str;
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            txtUserid = (TextView) findViewById(R.id.txtUserId);
            txtUserid.setText(result);
        }
    }
}

这篇关于调用Web API并在Android中接收返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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