HOW TO CREATE JSON FILE.

DefineMac Mac

Total Post:6

Points:42
Posted by  DefineMac Mac
 1022  View(s)
Ratings:
Rate this:
Anyone can give me an idea about json file with server, like getting a file text? or how to create a .json file so i can put it in asset folder as my text file.
  1. Manoj Pandey

    Post:26

    Points:188
    Re: How to create json file.

    Use below code for get json from any server

    class MyAsyncTask extends AsyncTask<String, String, Void> {

    private ProgressDialog progressDialog = new ProgressDialog(context);
    InputStream inputStream = null;
    String result = "";

    protected void onPreExecute() {
    progressDialog.setMessage("Please wait downloading your data...");
    progressDialog.show();
    progressDialog.setOnCancelListener(new OnCancelListener() {
    public void onCancel(DialogInterface arg0) {
    MyAsyncTask.this.cancel(true);
    }
    });
    }

    @Override
    protected Void doInBackground(String... params) {
    // set your url of json file
    String url_select = "http://yoururlhere.com";

    ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();

    try {
    // Set up HTTP post

    // HttpClient is more then less deprecated. Need to change to
    // URLConnection
    HttpClient httpClient = new DefaultHttpClient();

    HttpPost httpPost = new HttpPost(url_select);
    httpPost.setEntity(new UrlEncodedFormEntity(param));
    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();

    // Read content & Log
    inputStream = httpEntity.getContent();

    } catch (Exception e4) {

    e4.printStackTrace();
    }
    // Convert response to string using String Builder
    try {
    BufferedReader bReader = new BufferedReader(
    new InputStreamReader(inputStream, "utf-8"), 8);
    StringBuilder sBuilder = new StringBuilder();

    String line = null;
    while ((line = bReader.readLine()) != null) {
    sBuilder.append(line + "\n");
    }

    inputStream.close();
    result = sBuilder.toString();

    } catch (Exception e) {

    }
    return null;
    } // protected Void doInBackground(S

    protected void onPostExecute(Void v) {
    // parse JSON data
    try {
    JSONArray jArray = new JSONArray(result);
    for (int i = 0; i < jArray.length(); i++) {

    JSONObject jObject = jArray.getJSONObject(i);

    String name = jObject.getString("name");
    String tab1_text = jObject.getString("tab1_text");
    int active = jObject.getInt("active");

    } // End Loop
    this.progressDialog.dismiss();
     catch (Exception e)
    } // protected void onPostExecute(Void v)
    } // class MyAsync


  1. DefineMac Mac

    Post:6

    Points:42
    Re: How to create json file.

    how can i call that? sorry noob here

  1. Manoj Pandey

    Post:26

    Points:188
    Re: How to create json file.

    new MyAsyncTask() .execute();

      Modified On Jun-22-2015 04:16:33 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!