Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

android - Getting info from api using JSON

I'm having troubles reading from this api

http://api.xhanch.com/islamic-get-prayer-time.php?lng=67&lat=24&yy=2012&mm=7&gmt=5&m=json

Here's my code:

new Read().execute("sunrise");

public JSONObject retrieveInfo(String user) throws ClientProtocolException,IOException, JSONException {
        StringBuilder url = new StringBuilder(URL);
        url.append(user);

        HttpGet get = new HttpGet(url.toString());
        HttpResponse r = client.execute(get);
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
        JSONArray timeline = new JSONArray(data);
        JSONObject last = timeline.getJSONObject(1);
        return last;
    }

    public class Read extends AsyncTask<String, Integer, String> {

        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub
            try {
                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;
        }

This method always returns a blank string instead of the info I need.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

here goes a working example for you

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();

        }

    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...