0

I have a problem that bothers me for several hours.

I have a dialog that is based on notifying if there is a new version of the application where it shows me a message with the available version along with the changes in it.

I have this code:

public class MainActivity extends AppCompatActivity {

private static  String url = "localhost/version.json";
String VersionUpdate;
String Cambios;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new VersionCheck().execute();
}
private class VersionCheck extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();
        String jsonStr = sh.makeServiceCall(url);
        if (jsonStr != null){
            try {

                JSONObject jsonObj = new JSONObject(jsonStr);
                    JSONArray obtener = jsonObj.getJSONArray("Obtener");
                for (int i = 0; i < obtener.length(); i++)
                {
                    JSONObject v = obtener.getJSONObject(i);
                    VersionUpdate = v.getString("version");
                    Cambios = v.getString("cambios");
                }
            }catch (final JSONException e) {

                // Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Toast.makeText(getApplicationContext(),
                                "El formato de JSON esta errado: " + e.getMessage(),
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }
        } else {



            //Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "El servidor de comprobar versión esta caido.",
                            Toast.LENGTH_LONG)
                            .show();
                }
            });

        }
        return null;
    }

        @Override
        protected void onPostExecute (Void result){

        if (VersionUpdate != null) {

        super.onPostExecute(result);
        String VersionName = BuildConfig.VERSION_NAME;
        if (VersionUpdate.equals(VersionName)) {
            Toast.makeText(getApplicationContext(),
                    "Version actual: " + VersionName,
                    Toast.LENGTH_LONG)
                    .show();
        } else {

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Actualización");
            builder.setIcon(R.mipmap.ic_launcher);
            builder.setMessage("Nueva versión disponible" + "\n" + "Incluye: " + Cambios + "\n" + "Version disponible: " + VersionUpdate)
                    .setPositiveButton("UPDATE", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            final String appName = getPackageName();

                            try {
                                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appName)));
                            } catch (android.content.ActivityNotFoundException anfe) {
                                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appName)));
                            }

                            finish();

                        }
                    });

            AlertDialog alert = builder.create();
            alert.show();


            }
        }
    }
 }
}

And I have the JSON format on a web to read it, this would be:

{
"Obtener": [
   {
   "version": "1.2",
  "cambios": [ 
    "fox1",
    "fox2",
    "fox3",
    "fox4",
    "fox5",
    "fox6",
    "fox7",
    "fox8",
    "fox9",
    "fox10"
    ]
     }
 ] 
} 

Everything is going well! however there is a json reading that is not appropriate and he sends me this message like this:

enter image description here

My goal is to print the list "cambios" line by line without characters, example:

Nueva version disponible
Incluye:
Fox1
Fox2
Fox3
Fox4
Fox5
Fox6
Fox7
Fox8
Fox9
Fox10
Versión disponible: 1.2

Kind regards, and thanks in advance for your answers :D.

coder
  • 8,346
  • 16
  • 39
  • 53
Luis Rivas
  • 79
  • 5

2 Answers2

0

That is because you are casting a JSONArray into a String object. you should do the following to properly format JSONArray to String

Cambios = "";
JSONArray cambiosArr = v.getJSONArray("cambios");
for (int j = 0; j < cambiosArr.length(); j++) {
    Cambios += cambiosArr.getString(j) + "\n";
}

instead of your following line

Cambios = v.getString("cambios");
Fahed Yasin
  • 400
  • 2
  • 9
0

You can do it manually by for loop your json array and fill your array list by looping it

But it is better to use Gson library to convert your json string to pojos

check if Gson is added to your app build gradle file if not then add the below line of code in dependencies

compile 'com.google.code.gson:gson:2.8.1'

Follow Link

Then you need to have pojos for your json string

Download from here your json pojo class - Version Pojo

Then you just need to have 2 lines of code to get your version information into array of data and you can display in listview also

VersionData versionData = new Gson().fromJson("json string",VersionData.class);
List<Obtener> obtenerList=versionData.getObtener();

You can also use the dialog with the list item

Follow Link

Navin
  • 285
  • 1
  • 8