Flyzy Dev
Flyzy Dev

Reputation: 91

I can't use variable on String.format

When I set Ori (Example: Ori = "WSSS") using getOrigin() shows result. But When I set Ori = Info[1] then I can't call getOrigin(). I want to use getOrigin() (don't set value) to show result.

In main

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

Upvotes: 0

Views: 147

Answers (1)

Merlevede
Merlevede

Reputation: 8180

Your variable Info[] is declared but it is not initialized and contains no data.

Ori = Info[1]

is invalid!

You need to do something similar to what you're doing with Origin in this line.

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

Upvotes: 1

Related Questions