使用JSON从API获取信息

使用JSON从API获取信息,第1张

使用JSON从API获取信息

这为你提供了一个可行例子

import java.io.IOException;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import org.json.JSONException;import org.json.JSONObject;import android.app.Activity;import android.app.alertDialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.os.AsyncTask;import android.os.Bundle;public class GetPrayerTime extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.custom_component);        new Read().execute("sunrise");    }    public JSonObject retrieveInfo(String user) throws ClientProtocolException, IOException, JSonException {        StringBuilder url = new StringBuilder(     "http://api.xhanch.com/islamic-get-prayer-time.php?lng=67&lat=24&yy=2012&mm=7&gmt=5&m=json");        url.append(user);        HttpClient httpclient = new DefaultHttpClient();        HttpGet get = new HttpGet(url.toString());        HttpResponse r = httpclient.execute(get);        HttpEntity e = r.getEntity();        String data = EntityUtils.toString(e);        JSonObject timeline = new JSonObject(data);        return timeline.getJSonObject("1");    }    private class Read extends AsyncTask<String, Integer, String> {        ProgressDialog pd = null;        @Override        protected void onPreExecute() { super.onPreExecute(); pd = new ProgressDialog(GetPrayerTime.this); pd.setTitle("Downloading..."); pd.setMessage("Please wait..."); pd.setCancelable(false); pd.show();        }        protected String doInBackground(String... arg0) { // TODO Auto-generated method stub try {     JSonObject json = retrieveInfo("");     return json.getString(arg0[0]); } catch (ClientProtocolException e) {     // TODO Auto-generated catch block     e.printStackTrace(); } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace(); } catch (JSonException e) {     // TODO Auto-generated catch block     e.printStackTrace(); } return null;        }        @Override        protected void onPostExecute(String status) { super.onPostExecute(status); pd.dismiss(); alertDialog alertDialog = new alertDialog.Builder(         GetPrayerTime.this).create(); alertDialog.setTitle("Prayer time"); alertDialog.setMessage(status); alertDialog.setButton("OK", new DialogInterface.onClickListener() {     public void onClick(DialogInterface dialog, int which) {         GetPrayerTime.this.finish();         dialog.cancel();     } }); alertDialog.setIcon(android.R.drawable.ic_dialog_info); alertDialog.show();        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4924626.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-12
下一篇2022-11-12

发表评论

登录后才能评论

评论列表(0条)

    保存