0

我是 android 初学者并尝试使用 json 获取 pnr 状态,这是我的代码不起作用,请帮助我。 还告诉我哪种解析方法是goo xml parse或json parse。当你提出问题时,多用一些词来描述你的问题总是有帮助的。如果真的没什么好说的,就从网上随便抄一段,但一定要把它们标记为虚拟文本,这样人们就不会注意到它们。

 public class JSON extends Activity {
    String completeData="";
    TextView tv;
    EditText et;
    Button bt;
    HttpClient client;
    JSONObject jsonobj;
    final static String URI="http://pnrapi.alagu.net/api/v1.0/pnr/";
    String pnr_no=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json);
        tv=(TextView) findViewById(R.id.textView1);
        et=(EditText) findViewById(R.id.editText1);
        bt=(Button) findViewById(R.id.button1);
        client=new DefaultHttpClient();

    }

    public void showpnr(View v){
        pnr_no=et.getText().toString();
        if(pnr_no.equals("")){
            Toast.makeText(this, "Enter the Valid Pnr", Toast.LENGTH_LONG).show();
            return;
        }
            GetPNR pnr=new GetPNR();
            pnr.execute("train-name");
            completeData="";
        }
        public JSONArray pnr(String username){
            JSONArray jarray=null;
            try 
            {
                StringBuilder builder=new StringBuilder(URI);
                builder.append(username);
                HttpGet get=new HttpGet(builder.toString());
                HttpResponse response=client.execute(get);
                int status =response.getStatusLine().getStatusCode();
                if(status==200){
                    HttpEntity entity=response.getEntity();
                    String data=EntityUtils.toString(entity);
                    jarray=new JSONArray(data);
                }
                else{
                    Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
                    }
            }catch(ClientProtocolException e){
                e.printStackTrace();
            }
            catch(IOException e){
                e.printStackTrace();
            }
            catch(JSONException e){
                e.printStackTrace();
            }
            catch(Exception e){
                e.printStackTrace();
            }
            return jarray;
        }


    JSONObject js_pnr=new JSONObject();
    public class GetPNR extends AsyncTask<String, Integer, ArrayList<String>>
{



    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        tv.setText("Loading Pnr status");
        super.onPreExecute();
    }

    @Override
    protected ArrayList<String> doInBackground(String... params) {
        // TODO Auto-generated method stub
        ArrayList<String> al_texts=new ArrayList<String>();
        try{
            JSONArray data =pnr(pnr_no);
            if(data==null){
                return null;
            }
            int count=data.length();
            JSONObject jobj=new JSONObject();
            for(int i=0;i<count;i++){
                jobj=data.getJSONObject(i);
                al_texts.add(jobj.getString("train-name").toString());
            }
            return al_texts;
        }catch(JSONException e){
            e.printStackTrace();
        }

        return null;
    }
    @Override
    protected void onPostExecute(ArrayList<String> al_pnrText) {
        if(al_pnrText==null){
            tv.setText("Pnr not found");
            return;
        }
        for(String string:al_pnrText){
            completeData+=string+System.getProperty("line.seperator")
                          +System.getProperty("line.seperator");
        }
        tv.setText("pnr status:"+System.getProperty("line.seperator")+completeData);
        }
}   
}
4

1 回答 1

0

在你的按钮 onclick 里面写:

bt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String details = "";
            GetPNR pnrDetails = new GetPNR();
            try {
                details = pnrDetails.execute(URI+et.getText().toString()).get();


                    Log.d("train", details);
                    tv.setText(details);


            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

Asynctask 就像:

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


    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        String al_texts = "";

        for(String newUrl:params){
            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(newUrl);
            HttpResponse response;
            try {
                response = client.execute(get);
                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                String webData = "";

                while((webData = reader.readLine()) != null){
                     Log.i("data", webData);
                     JSONObject myAwway = new JSONObject(webData);

                    JSONObject data = myAwway.getJSONObject("data");
                    Log.i("data", data.toString());
                    JSONObject travelDate = data.getJSONObject("travel_date");
                    JSONObject from = data.getJSONObject("from");
                    JSONObject to = data.getJSONObject("to");
                    JSONObject alright = data.getJSONObject("alight");
                    JSONObject board = data.getJSONObject("board");
                    JSONArray passenger = data.getJSONArray("passenger");

                    al_texts = data.getString("train_name");
                    Log.i("data", al_texts);
                }
            } 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 al_texts;

    }
}

在这里我只显示/返回一个字符串(火车名称)。像这样你可以显示每个细节。

这是您修改后的代码并且工作正常。

于 2013-10-02T08:33:13.057 回答