0

This is my code for getting the JSON file in web:

package com.example.maclocation;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {
String[] n1;
String[] n2;
String[] n3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new ActorsAsyncTask().execute("link here");
}
class ActorsAsyncTask extends AsyncTask<String, Void, Boolean> {

This is my code for getting the JSON file in web:

@Override
    protected Boolean doInBackground(String... urls) {
        try {

            //------------------>>
            HttpPost post = new HttpPost(urls[0]);
            HttpClient client = new DefaultHttpClient();
            HttpResponse response = client.execute(post);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);
                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.optJSONArray("coordinates");
                n1 = new String[jarray.length()];
                n2 = new String[jarray.length()];
                n3 = new String[jarray.length()];
                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject object = jarray.getJSONObject(i);
                    n1[i] = object.getString("place").toString();
                    n2[i] = object.getString("lat").toString();
                    n3[i] = object.getString("lng").toString();
                }
                return true;
            }
            //------------------>>
        } catch (ParseException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

This is my code for getting the JSON file in web:

    protected void onPostExecute(Boolean result) {
        if(result == false){
            Toast.makeText(getApplicationContext(), "Unable to fetch data           from server", Toast.LENGTH_LONG).show();
        }else{
            for (int i = 0; i < n1.length; i++) {
      //                String xss= actorsList.
            Toast.makeText(getApplicationContext(), n1[i] + "*" + n2[i]+ "*"    +n3[i] , Toast.LENGTH_LONG).show();
            }

        }
    }
}
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Question:

Is it possible to update the JSON file that is hosted in web server? Like updating my current location? I am using ADT Eclipse.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
DefineMc
  • 1
  • 5

1 Answers1

0

So, you need to code it on the server side, using probably php, ruby, python, or any other language you may be using on the server..

And then through the app you can make an http request passing some parameter to identify which action you are taking on the same server where the JSON file is hosted (so you can reach it) and then you can edit it!

This tutorial is very good: http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/

In this case it is a database, not an JSON file, but the idea is the same.

caiolopes
  • 561
  • 8
  • 14
  • oh great, thanks, now my problem is, i dont have idea when it comes to php – DefineMc Jun 23 '15 at 05:31
  • [Here](http://stackoverflow.com/questions/17806224/how-to-update-edit-json-file-using-php) and [here](http://stackoverflow.com/questions/6313276/php-how-to-update-json-data-in-a-txt-file). It is super easy, basically you need to use json_decode() and json_encode() functions. – caiolopes Jun 23 '15 at 18:52
  • So, you can have a php page that reads an request (can be POST or GET) from the app, for instance: http://www.example.com/?action=write_json&content=something. The php reads the action, if it is 'write_json', you get the content which is 'something' and write it on the json file. – caiolopes Jun 23 '15 at 19:02
  • @ciolopes but is it possible to update the json file directly from app? – DefineMc Jun 23 '15 at 22:47
  • You can do if the json file is inside the app, stored on the phone's internal storage. If the json is on a web server, this is the only way I can think of, but I may be wrong.. – caiolopes Jun 25 '15 at 22:26