Home > DeveloperSection > Forums > How to create json file.
DefineMac Mac

Total Post:6

Posted on    June-18-2015 6:15 PM

 Android Android  Android Json 

 3 Reply(s)
 948  View(s)
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.

Manoj Pandey

Total Post:26

Posted on    June-19-2015 7:11 AM

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.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface arg0) {

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) {

// 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");

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
 catch (Exception e)
} // protected void onPostExecute(Void v)
} // class MyAsync

DefineMac Mac

Total Post:6

Posted on    June-21-2015 8:29 PM

how can i call that? sorry noob here

Manoj Pandey

Total Post:26

Posted on    June-21-2015 11:12 PM

new MyAsyncTask() .execute();

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

Don't want to miss updates? Please click the below button!

Follow MindStick