我用谷歌搜索了这些主题,但没有得到任何有用的信息。
我想在我的 android 项目中使用 Web API,但不知道如何从 android 或 java 调用它们
我有一些 WEB APIthis网站并想在我的 android 项目中使用。
例如,一个网络 APIURL : /api/uname2uid/{uname}.
那个转换用户名 to 用户数字 ID
我想显示User ID从该 API 返回到TextView
。所以我需要返回值作为字符串或数字。请帮我这个。
EDIT
我只是想学习如何使用WEB API
为了更简单:在我的程序中我有一个TextView
, aEditText
and a Button
当我按下Button
then TextView
会更新对应的user ID for Username给出EditText
领域仅此而已。一个有效的例子将有助于我理解这一点。
一个简单的异步调用就可以为你做这件事:
MainActivity.java
new YourAsync(){
protected void onPostExecute(Object[] result) {
String response = result[1].toString();
try{
JSONObject jsonObject=(new JSONObject(jsonResult)).getJSONObject("data"); //Parsing json object named 'data'
yourTextView.setText(jsonObject.getString("uname"));
}.execute(YourURL);
YourAsync.java
public class YourAsync extends AsyncTask<String, Integer, Object[]> {
@Override
protected Object[] doInBackground(String... params) {
// TODO Auto-generated method stub
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0].toString());
Log.d("http client post set","");
try{
HttpResponse response = httpclient.execute(httppost);
Log.d("YourAsync","Executed");
return new Object[]{response, new BasicResponseHandler().handleResponse(response)};
}catch(Exception e){
Log.d("" + e, "");
}
return new Object[0];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)