-2
    try {
        final List<NetworkType> dataReceived = getData();
        int i = 0;
        //Array Iteration
        for(final NetworkType networkType : dataReceived) {
            i++;
            if (i > 3) {
                Toast.makeText(getApplicationContext(), "Done!!", Toast.LENGTH_SHORT);
            } else {
                String mCell1CID = networkType.getCell1CID();
                String mCell1LAC = networkType.getCell1LAC();
                String mCell1MCC = networkType.getCell1MCC();
                String mCell1MNC = networkType.getCell1MNC();
                String mCell1CI = networkType.getCell1CI();
                String mCell1TAC = networkType.getCell1TAC();

                //API requestBody
                String url = "https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyA8NAA3dUpECFn2j6pdcQT3wgqUM98UZ2Q";
                String cellID = null;
                String locationAreaCode = null;
                if (mCell1CID.equals("") && mCell1LAC.equals("")) {
                    cellID = mCell1CI;
                    locationAreaCode = mCell1TAC;
                } else if (mCell1CI.equals("") && mCell1TAC.equals("")) {
                    cellID = mCell1CID;
                    locationAreaCode = mCell1LAC;
                } else {
                    Log.d("GeoL", "3");
                }
                String requestBody =
                        "{" +
                                "\"cellTowers\":[" +
                                "{" +
                                "\"cellId\"             :" + cellID + "," +
                                "\"locationAreaCode\"   :" + locationAreaCode + "," +
                                "\"mobileCountryCode\"  :" + "\"" + mCell1MCC + "\"" + "," +
                                "\"mobileNetworkCode\"  :" + "\"" + mCell1MNC + "\"" +
                                "}" +
                                "]" +
                                "}";

                // Instantiate the RequestQueue.
                RequestQueue queue = Volley.newRequestQueue(this);
                JsonObjectRequest jsObjRequest = new JsonObjectRequest
                        (Request.Method.POST, url, requestBody, new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                JSONObject jsonObject;
                                try {
                                    jsonObject = new JSONObject(String.valueOf(response));
                                    JSONObject latitude = jsonObject.getJSONObject("location");
                                    String lat = latitude.getString("lat");
                                    String lng = latitude.getString("lng");
                                    String acc = jsonObject.getString("accuracy");
                                    if (!dataReceived.listIterator().hasNext()){
                                        String print;
                                        print = String.format("%s%s", print, print(lat.toString(), lng.toString(), acc.toString()));
                                        writeToFile(print, getApplicationContext());
                                    } else {
                                        String print;
                                        print = String.format("%s%s,", print, print(lat.toString(), lng.toString(), acc.toString()));
                                        writeToFile(print, getApplicationContext());
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }, new Response.ErrorListener() {

                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Log.e("Response", "error" + error.toString());
                            }
                        });
                queue.add(jsObjRequest);
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

I need to concat the received value to the print variable. But this the error which i receive print needs to be initialized is what I receive. also if declared outside the try/catch block it requires it to be final. What to do?

  • 1
    Paste your entire code. – cs95 Jun 23 '17 at 07:43
  • pasted, kindly recheck – Pratyush Pant Jun 23 '17 at 07:51
  • Okay, so where's the loop inside that function? What you've defined is a callback. What should `print` contain before invocation? What is its purpose? – cs95 Jun 23 '17 at 07:52
  • one print is the variable, the other one is method. I want to append all strings received in the loop to the print variable, which should be formatted according to print method. – Pratyush Pant Jun 23 '17 at 07:58
  • Okay, in that case you should take a look at [this](https://stackoverflow.com/questions/33896030/how-do-i-use-a-variable-outside-the-onresponse-in-android). Also, it makes no sense to keep redeclaring the variable if you want to append to it each time. – cs95 Jun 23 '17 at 08:02
  • I cannot use it outside the onResponse, coz the data received needs to be recorded each time the loop is iterated. so i think this also wont help. Thanks though :) – Pratyush Pant Jun 23 '17 at 08:11
  • So that's the thing. You want to append it at each iteration. But how can you? `print` is a local variable to the callback function and therefore has _no idea_ it is being called inside a loop. So what you want is a variable that is local, but also retains information each time it is called. That is not possible. – cs95 Jun 23 '17 at 08:17
  • so is there any other way with which i can get this done – Pratyush Pant Jun 23 '17 at 08:25
  • The link I pasted shows how to access this variable from outside the function, so you can concatenate each time without having to redeclare it. Declare `print` ONCE outside the loop, then use it. – cs95 Jun 23 '17 at 08:31

2 Answers2

0

You are providing a value that is not initialized to the String.format

You need to initialize the variable "print" with what you want to be used in the String.format (for example an empty String) :

String print= ""

Or use String Builder:

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("1");
stringBuilder.append("2");

String finalString = stringBuilder.toString();
Adib Faramarzi
  • 3,798
  • 3
  • 29
  • 44
  • i need to append each value received in the loop. so initializing it to null would not work – Pratyush Pant Jun 23 '17 at 07:50
  • I initialized it with an empty string , not null. – Adib Faramarzi Jun 23 '17 at 07:56
  • Also since you updated the question, i suggest using moshi or Gson for easily creating and reading json data. – Adib Faramarzi Jun 23 '17 at 07:57
  • already using gson but for some other json file. as far as initializing is concerned, either of the initializations, the null or an empty string wont work because the value of the variable would be refreshed each time the loop is iterated. Therefore null or empty string wont work. Also initialiazing it outside the loop then asks me to declare it final. Which again is not the solution. – Pratyush Pant Jun 23 '17 at 08:03
  • I added another solution if your goal is to just concatenate them. – Adib Faramarzi Jun 23 '17 at 08:15
  • i tried stringbuilder as well, but it didnt work because of the the data I receive and parameters in the print method. I did change them but how to change data received from api to stringbuilder. – Pratyush Pant Jun 23 '17 at 08:22
  • just pass whatever you are appending to the string builder , e.g. `print(lat.toString(), lng.toString(), acc.toString())` – Adib Faramarzi Jun 23 '17 at 08:50
0

just use String print = null; when you declare print

Shubham Goel
  • 1,962
  • 17
  • 25