我不能在String.format上使用变量

时间:2014-03-05 05:36:37

标签: java android json string

当我使用Ori设置Ori = "WSSS"(示例:getOrigin())时显示结果。但是当我设置Ori = Info[1]时,我无法调用getOrigin()。我想使用getOrigin()(不设置值)来显示结果。

主要

    {    
            String user = "abc";
            String pw = "123";
            String url,url_origin;
            String Info[],Origin[]={"",""};
            String Ori;

            @SuppressLint("NewApi")
            @Override
            protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.details_layout);

            StrictMode.ThreadPolicy policy = new StrictMode.
            ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            String ID = "HAAB";
            url = String.format("http://flightxml.flightaware.com/json/FlightXML2/InFlightInfo?ident=%s",ID);

            getDetails(url);
            Ori = Info[1];
            url_origin = String.format("http://flightxml.flightaware.com/json/FlightXML2/AirportInfo?airportCode=%s",Ori);
            getOrigin(url_origin);

            TextView fd1 = (TextView)findViewById(R.id.textView1);
            fd1.setText(Origin[1]);
    }

    public String[] getDetails(String url) 
    {  
             try {

                String result  = HttpGet(url);
                JSONObject json = new JSONObject(result);
                JSONObject val = json.getJSONObject("InFlightInfoResult");     

                Info[0] += String.format("%s\n", val.getString("type"));
                Info[1] += String.format("%s\n", val.getString("origin"));


                  } catch (JSONException e) {
                          e.printStackTrace();
                  } 

                        return Info;
                }

     public String[] getOrigin(String url) {  
            try {

                    String result  = HttpGet(url);
                    JSONObject json = new JSONObject(result);
                    JSONObject val = json.getJSONObject("AirportInfoResult");      

                    Origin[0] += String.format("%s\n", val.getString("name"));
                    Origin[1] += String.format("%s\n", val.getString("location"));

                      } catch (JSONException e) {
                          e.printStackTrace();
                      } 

                        return Origin;
                }

            // Connect Http Get //
                public String HttpGet(String url) {
                    StringBuilder builder = new StringBuilder();
                    HttpClient client = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet(url);
                    httpGet.addHeader("Authorization", "Basic " + Base64.encodeToString((user + ":" + pw).getBytes(), Base64.NO_WRAP));

                    try {
                        HttpResponse response = client.execute(httpGet);
                        StatusLine statusLine = response.getStatusLine();
                        int statusCode = statusLine.getStatusCode();
                        if (statusCode == 200) { // Status OK
                            HttpEntity entity = response.getEntity();
                            InputStream content = entity.getContent();
                            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                            String line;
                            while ((line = reader.readLine()) != null) {
                                builder.append(line);
                            }
                        } else {
                            Log.e("Log", "Failed to download result..");
                        }
                    } catch (ClientProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return builder.toString();
                }       }

1 个答案:

答案 0 :(得分:1)

您的变量Info[]已声明,但未初始化且不包含任何数据。

Ori = Info[1]

无效!

您需要执行与此行中Origin相似的操作。

String Info[],Origin[]={"",""};