user6880336 user6880336 - 1 year ago 97
Android Question

How to fetch a data and set it to TextView?

I want to fetch a data from the mysql database I tried but not getting the output also tried tutorials.

Here my complete code:-

package com.example.sachin.splashlogin;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Home extends Fragment {
Activity activity;

public Home() {};

TextView visit;
SessionManager session;
CardView cardviewvisit, cardvieworder, cardviewpayment, cardviewdelivery;
JSONObject jsonobject;
private static String url_visitor = "";
JSONParser jParser = new JSONParser();
JSONArray ownerObj, jsonarray;

ArrayList<HashMap<String, String>> arraylist;
ArrayList<String> v_username = new ArrayList<String>();
ArrayList<String> v_parties = new ArrayList<String>();
ArrayList<String> v_date = new ArrayList<String>();
String suid, uid, wt_wod_code1, wt_party1;
View view;

public void onCreate(Bundle savedInstanceState) {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_home, container, false);
cardviewvisit = (CardView) view.findViewById(;
cardvieworder = (CardView) view.findViewById(;
cardviewpayment = (CardView) view.findViewById(;
cardviewdelivery = (CardView) view.findViewById(;
session = new SessionManager(getActivity());
HashMap<String, String> user = session.getUserDetails();
uid = user.get(SessionManager.KEY_ID);
visit = (TextView) view.findViewById(;
cardviewvisit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

Fragment fragment = new DeliveryTab();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(, fragment).commit();


new DownloadJSON().execute();
return view;

private class DownloadJSON extends AsyncTask<Void, Void, Void> {

protected Void doInBackground(Void... args) {

// Create an array
try {
arraylist = new ArrayList<HashMap<String, String>>();

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("v_username", uid));
JSONObject json = jParser.makeHttpRequest(url_visitor, "GET", params);

int success1 = Integer.parseInt(json.getString("success4"));
Log.d("success4", json.toString());

if (success1 == 0) {
Snackbar.make(view, "Not Data Found", Snackbar.LENGTH_LONG).show();
if (success1 == 1) {
ownerObj = json.getJSONArray("visit");
for (int i = 0; i < ownerObj.length(); i++) {
jsonobject = ownerObj.getJSONObject(i);
} catch (Exception e) {
return null;

protected void onPostExecute(Void args) {



Here is my logcat, the data is displaying in this.

10-12 19:40:58.222 31209-31277/com.example.sachin.splashlogin D/P: [v_username=JAYESHBHAI]
10-12 19:40:58.222 31209-31277/com.example.sachin.splashlogin E/Json Object:: {"visit":[{"v_parties":"WONDAR MOBILE"}],"success":1}

I wanted to print parties name in the TextView.

If needed I'll also post my API in this..

please help me out from this..

Answer Source

Example to access:

JSONObject json = new JSONObject(YourStringJSON_VARIABLE);

 String parties = json.getString("v_parties");


To access a Array:

 JSONArray listArray = result.getJSONArray("your_list");
 for(int i = 0; i < listArray.length(); i++) {
                JSONObject jsonOb = listArray.getJSONObject(i);
                //store your variable
                String something = jsonOb.getString("something");

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download