Roan
Roan

Reputation: 29

Java get JSON in Android TextView from URL

I want to get a JSON response from a php displayed in a TextView in Android Studio. I now have this code to do that, but it doesn't work. As far as I can see it doesn't even run when the app is opened.

public class MainActivity extends Activity {

private SQLiteHandler db;
private SessionManager session;

private String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
    }
    return sb.toString();
}

public JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String jsonText = readAll(rd);
        JSONObject json = new JSONObject(jsonText);
        return json;
    } finally {
        is.close();
    }
}

public void main(String[] args) throws IOException, JSONException {
    TextView txtUser = (TextView) findViewById(R.id.user);
    JSONObject json = readJsonFromUrl("http://piggybank.wordmediavormgever.nl/getSaldo.php");
    System.out.println(json.toString());
    System.out.println(json.getString("saldo"));
    try {
        JSONObject jsonObject = new JSONObject();
        String response = jsonObject.getString("saldo");
        txtUser.setText(response);

    } catch (JSONException e) {

        e.printStackTrace();
    }
}

Can anyone see what I'm doing wrong? The response from the url is {"saldo":783}.

Upvotes: 0

Views: 4210

Answers (2)

Nguyễn Trung Hiếu
Nguyễn Trung Hiếu

Reputation: 2032

Try it!

remove

   JSONObject jsonObject = new JSONObject();

And use

   JSONObject json = readJsonFromUrl("http://piggybank.wordmediavormgever.nl/getSaldo.php");
  try {
    String response = json.getString("saldo");
    Log.e("AAAAAAAAA %s", response);

} 

You must call it in AsyncTask. Completed code!!!

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test2);
    new GetDataSync().execute();
}

String saldo = "";

public class GetDataSync extends AsyncTask<Void, Void, Void> {


    @Override
    protected Void doInBackground(Void... params) {
        try {
            getData();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        txtUser.setText(saldo);
    }
}

private void getData() throws IOException, JSONException {
    JSONObject json = readJsonFromUrl("http://piggybank.wordmediavormgever.nl/getSaldo.php");
    try {
        String response = json.getString("saldo");
        Log.e("AAAAAAAAA %s", response);
        saldo = response;

    } catch (JSONException e) {

        e.printStackTrace();
    }
}

private String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
    }
    return sb.toString();
}

public JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String jsonText = readAll(rd);
        JSONObject json = new JSONObject(jsonText);
        return json;
    } finally {
        is.close();
    }
}

Upvotes: 1

Navin Gupta
Navin Gupta

Reputation: 803

I think the problem is here in main function in try block

JSONObject jsonObject = new JSONObject();
String response = jsonObject.getString("saldo");
txtUser.setText(response);

your jsonObject is empty You should call

String response = json.getString("saldo");
txtUser.setText(response);

One more thing when you are making some network call you should do it in background thread , not on UI thread.(readJsonFromUrl method should be called in background thread)

as Nguyễn Trung Hiếu's answer suggested

Upvotes: 1

Related Questions