I can't find exactly where I have my problem or why But my app seems o crash every time it tries to search for data in the database. Why does this happen? On second thought, I think I might have messed up on MySingleton.class, but I'm not sure how.
UPDATE
It now does not crash, but still doesn't load the data from my database..
UPDATE 2
I get this error in my URL
Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in /storage/ssd1/078/5480078/public_html/denuncias/Connection.php on line 9
error
Connection.php
<?php
$server_name = "localhost";
$user_name = "id5480078_denuncias";
$password = "smoothcriminal1";
$conn = mysqli_connect($server_name, $user_name, $password) or die ('Server '. mysql_error());
$database_name= 'id5480078_denuncias';
mysqli_select_db($database_name) or die('error');
mysqli_query("SET NAMES 'utf8'");
?>
UPDATE 3 Connection.php was fixed, but nnow the data isnt loading in the app.
Error Logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: co.quindio.sena.navigationdrawerejemplo, PID: 25122
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ProgressDialog.show()' on a null object reference
at co.quindio.sena.navigationdrawerejemplo.Main2Activity.getSqlDetails(Main2Activity.java:58)
at co.quindio.sena.navigationdrawerejemplo.Main2Activity.access$000(Main2Activity.java:25)
at co.quindio.sena.navigationdrawerejemplo.Main2Activity$1.onClick(Main2Activity.java:48)
at android.view.View.performClick(View.java:5714)
at android.widget.TextView.performClick(TextView.java:10926)
at android.view.View$PerformClick.run(View.java:22589)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Main2Activity.class :
package co.quindio.sena.navigationdrawerejemplo;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import co.quindio.sena.navigationdrawerejemplo.R;
public class Main2Activity extends AppCompatActivity {
TextView result;
EditText phone;
Button search;
String number;
private ProgressDialog pd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
search = (Button)findViewById(R.id.button);
result = (TextView)findViewById(R.id.textView2);
phone = (EditText)findViewById(R.id.phone);
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getSqlDetails();
}
});
}
private void getSqlDetails() {
String url= "https://luisalonsoriveraibarra.000webhostapp.com/denuncias/read.php";
StringRequest stringRequest = new StringRequest(Request.Method.GET,
url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
pd.hide();
try {
JSONArray jsonarray = new JSONArray(response);
for(int i=0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String id = jsonobject.getString("id");
String price = jsonobject.getString("price");
String name = jsonobject.getString("name");
String phone = jsonobject.getString("phone");
result.setText(" ID -"+id+"\n Price -"+price+"\n Name -"+name+"\n Phone -"+phone);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
if(error != null){
Toast.makeText(getApplicationContext(), "Something went wrong.", Toast.LENGTH_LONG).show();
}
}
}
);
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);
}
}
MySingleton.class:
package co.quindio.sena.navigationdrawerejemplo;
import android.content.Context;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
/**
* Created by Luis Alonso on 17/7/2018.
*/
public class MySingleton {
private static MySingleton instance = null;
private RequestQueue mRequestQueue;
//a private constructor so no instances can be made outside this class
private MySingleton(Context context) {
mRequestQueue = Volley.newRequestQueue(context.getApplicationContext());
}
//Everytime you need an instance, call this
//synchronized to make the call thread-safe
public static synchronized MySingleton getInstance(Context applicationContext) {
if(instance == null)
instance = new MySingleton(applicationContext);
return instance;
}
//Initialize this or any other variables in probably the Application class
public void init(Context context) {}
public RequestQueue addToRequestQueue(StringRequest stringRequest) {
return mRequestQueue;
}
}
read.php:
include_once("Connection.php");
$query="SELECT * FROM data";
$result = mysqli_query($conn, $query);
while(($row = mysqli_fetch_assoc($result)) == true){
$data[]=$row;
}
echo json_encode($data);
Initialize progress dialog first before using it.
pd = new ProgressDialog (this);
Answer to updated Question:-
The method
mysqli_select_db(connection, dbname);
accepts two parameters. So do like this -
mysqli_select_db($conn, $database_name);
Related
I am trying to save data on button toggle, so that if pokemon is caught, when i run the application again, pokemon is caugt, and when released, It toggles back to "catch". But all my pokemon are displaying caught (button shows "release")
package com.example.pokedex;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class PokemonActivity extends AppCompatActivity {
private TextView nameTextView;
private TextView numberTextView;
private TextView type1TextView;
private TextView type2TextView;
private String url;
private RequestQueue requestQueue;
Button button;
SharedPreferences sharedpreferences;
public static final String mypreference = "mypref";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pokemon);
requestQueue = Volley.newRequestQueue(getApplicationContext());
url = getIntent().getStringExtra("url");
nameTextView = findViewById(R.id.pokedex_name);
numberTextView = findViewById(R.id.pokedex_number);
type1TextView = findViewById(R.id.pokedex_type);
type2TextView = findViewById(R.id.pokedex_type2);
button = findViewById(R.id.catcher);
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
load();
}
public void load() {
type1TextView.setText("");
type2TextView.setText("");
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, response -> {
try {
nameTextView.setText(response.getString("name"));
numberTextView.setText(String.format("#%03d", response.getInt("id")));
JSONArray typeEntries = response.getJSONArray("types");
for (int i = 0; i < typeEntries.length(); i++) {
JSONObject typeEntry = typeEntries.getJSONObject(i);
int slot = typeEntry.getInt("slot");
String type = typeEntry.getJSONObject("type").getString("name");
if (slot == 1) {
type1TextView.setText(type);
} else if (slot == 2) {
type2TextView.setText(type);
}
}
} catch (JSONException e) {
Log.e("cs50", "Pokemon json error", e);
}
}, error -> Log.e("cs50", "Pokemon details error", error));
requestQueue.add(request);
if (sharedpreferences.contains(nameTextView.getText().toString()))
button.setText("Release");
}
public void toggleCatch(View view) {
String name = nameTextView.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
if (button.getText().toString().toLowerCase().equals("Catch".toLowerCase())) {
//editor.clear();
editor.putString(name, name);
editor.apply();
editor.commit();
button.setText("Release");
} else if (button.getText().toString().toLowerCase().equals("Release".toLowerCase())){
editor.remove(name);
editor.apply();
editor.commit();
button.setText("Catch");
}
}
}
I have tried everything i know how to, please help.
Unmodified source code at "https://cdn.cs50.net/2019/fall/tracks/android/pokedex/pokedex.zip"
The following line of code
if (sharedpreferences.contains(nameTextView.getText().toString()))
button.setText("Release");
should go into your onResponseListener() that is your response -> ....
Currently you are calling the if statement above synchronously. This means that you are searching for not any valid Pokemon name but "" (empty string) inside SharedPreferences. In one of your tests, you probably saved empty string as a key, and you cannot undo that because any transaction after you load your pokemons includes a non-empty string Pokemon name.
If taht is indeed the case, you can easily add a temporary code inside your activity onCreate() method to remove that entry from SharedPreferences.
This is my code for getting the data from JSON, it will contain only one row,
but it will give output for only "pro_name" for left of the values it will give just dots(......).
package com.example.sh_enterprises;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class Detailes extends AppCompatActivity {
//this is the JSON Data URL
//make sure you are using the correct ip else it will not work
public static String URL_PRODUCTS, pass1,pass2 ;
public static String ptopic,psubcode;
private static ProgressDialog progressDialog;
//a list to store all the products
List<pro_data> productList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailes);
Intent i = getIntent();
pass1 = i.getStringExtra("pro_id");
//Toast.makeText(this,"HI THIS IS "+pass,Toast.LENGTH_SHORT).show();
//URL_PRODUCTS = "https://premnathindia.000webhostapp.com/Api.php?p1="+pass;
URL_PRODUCTS = "http://192.168.225.25/prem/Api.php?p1="+pass1+"&p2=det";
Toast.makeText(this, URL_PRODUCTS, Toast.LENGTH_SHORT).show();
productList = new ArrayList<>();
//this method will fetch and parse json
//to display it in recyclerview
loadProducts();
}
private void loadProducts() {
prefConfig product = new prefConfig(this);
String str = product.read_ret_id();
Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please wait...");
progressDialog.show();
/*
* Creating a String Request
* The request type is GET defined by first parameter
* The URL is defined in the second parameter
* Then we have a Response Listener and a Error Listener
* In response listener we will get the JSON response as a String
* */
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
//converting the string to json array object
JSONArray array = new JSONArray(response);
//traversing through all the object
for (int i = 0; i < 1; i++) {
//getting product object from json array
JSONObject product = array.getJSONObject(i);
product.getString("pro_id");
String st;
TextView a = findViewById(R.id.pro_name);
TextView b = findViewById(R.id.weight);
TextView c = findViewById(R.id.base_amount);
TextView d = findViewById(R.id.mass_amonut);
TextView e = findViewById(R.id.brand);
TextView f = findViewById(R.id.catogery);
String st1 = product.getString("pro_name");
a.setText(st1);
st = product.getString("weight");
b.setText(st);
st = product.getString("total_amount");
d.setText(st);
//st = product.getString("pro_img_url");
st = product.getString("base_amount");
c.setText(st);
//st = product.getString("disc");
st = product.getString("brands");
e.setText(st);
st = product.getString("categories");
f.setText(st);
}
progressDialog.dismiss();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
public void downme_ooi(View view) {
String pro_id = ((TextView) view).getText().toString();
Intent i = new Intent(this,Detailes.class);
i.putExtra("pro_id",pro_id);
startActivity(i);
}
public void go_back(View view) {
super.onBackPressed();
}
}
Sorry I made the Mistake that in TextView I put the text as password .
how should i post this one in next activity by clicking the button Retrieve
But i dont know how to code this one and im using online database 000webhost. my problem is when i click the button retrieve it will post the content of database in next activity which is print.class from MainActivity.class this is my code as far
MainActivity.class
package com.example.child.retrieve;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.android.volley.NetworkResponse;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.Volley;
import com.example.child.retrieve.LoginRequest;
import com.example.child.retrieve.R;
import com.example.child.retrieve.print;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button bLogin = (Button)findViewById(R.id.ret);
bLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Response.Listener<String> responseListener = new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if(success) {
Intent intent = new Intent(MainActivity.this, print.class);
MainActivity.this.startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(responseListener);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
queue.add(loginRequest);
}
});
}
/* import com.android.volley.toolbox.HttpHeaderParser; */
public void onErrorResponse(VolleyError error) {
// As of f605da3 the following should work
NetworkResponse response = error.networkResponse;
if (error instanceof ServerError && response != null) {
try {
String res = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, "utf-8"));
// Now you can use any deserializer to make sense of data
JSONObject obj = new JSONObject(res);
} catch (UnsupportedEncodingException e1) {
// Couldn't properly decode data to string
e1.printStackTrace();
} catch (JSONException e2) {
// returned data is not JSONObject?
e2.printStackTrace();
}
}
}
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.child.retrieve.MainActivity">
<Button
android:id="#+id/ret"
android:text="Retrieve"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
The LoginRequest.class which is the database is being loaded
package com.example.child.retrieve;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Child on 1/31/2017.
*/
public class LoginRequest extends StringRequest {
private static final String LOGIN_REQUEST_URL = "http://cedportfolio.000webhostapp.com/Login.php";
private Map<String, String> params;
public LoginRequest(Response.Listener<String> listener) {
super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
params = new HashMap<>();
}
#Override
public Map<String, String> getParams() {
return params;
}
}
print.class
package com.example.child.retrieve;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by Child on 7/21/2017.
*/
public class print extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.print);
}
}
If you want to pass data from one activity to other you need to create a Parcelable object. Check this answer
You will nedd something like this:
public class MyData implements Parcelable {
...
}
Parse the response and pass it to the new activity
MyData data = parseJson(jsonResponse)
Intent intent = new Intent(MainActivity.this, print.class);
intent.putParcelable("data", data);
MainActivity.this.startActivity(intent);
Recover it at the new activity
public class print extends Activity {
...
MyData data = getIntent().getParcelableExtra("data");
...
}
Beware that passing big objects between activities may have performance penalties. You should consider other strategies like passing some IDs and fetching the data at the "receiver" activity
I'm new in creating Android apps. What I want to do is to pass variable to php service and get true or false. This is my code
package com.fishingtournaments.tournamentapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class LoginActivity extends AppCompatActivity {
TextView ats;
EditText qrCode_edit;
Button check_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ats = (TextView) findViewById(R.id.text_textView);
qrCode_edit = (EditText) findViewById(R.id.qrCode_editText);
check_button = (Button) findViewById(R.id.check_button);
check_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String qrCode = qrCode_edit.getText().toString();
//ats.setText(qrCode);
Response.Listener<String> responseListener = new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success){
ats.setText("Toks vartotojas yra");
}else{
ats.setText("Tokio vartotojo nera");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest;
loginRequest = new LoginRequest(qrCode, responseListener);
RequestQueue queue;
queue = Volley.newRequestQueue(LoginActivity.this);
queue.add(loginRequest);
}
});
}
}
This is my LoginRequest class
package com.fishingtournaments.tournamentapp;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.Map;
/**
* Created by manta on 2017-02-08.
*/
public class LoginRequest extends StringRequest {
public static String LOGIN_REQUEST_URL = "http://suvalkijossapalas.devpanda.eu/checkQr.php";
public Map<String, String> params;
public LoginRequest(String qrcode, Response.Listener<String> listener){
super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
params.put("qrCode",qrcode);
}
public Map<String,String> getParams(){
return params;
}
}
When I try to run app on emulator and press Check button my app crashes and I get this error:
java.lang.NullPointerException: Attempt to invoke interface method
'java.lang.Object java.util.Map.put(java.lang.Object,
java.lang.Object)' on a null object reference
at
com.fishingtournaments.tournamentapp.LoginRequest.(LoginRequest.java:19)
at
com.fishingtournaments.tournamentapp.LoginActivity$1.onClick(LoginActivity.java:64)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
I think this might problem with:
loginRequest = new LoginRequest(qrCode, responseListener); I think it crashes because of the responseListener.
Any help will be appreciated
The problem is in your LoginRequest code. Specifically this line:
params.put("qrCode",qrcode);
Here the params field equals to null.
You declare correctly your params field as a Map<String String> but you never create a new instance of Map<String, String> to hold the data.
You will have to initialise your field in the constructor, like this:
public LoginRequest(String qrcode, Response.Listener<String> listener){
super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
params = new Map<String, String();
params.put("qrCode",qrcode);
}
Or initialise the field immediately after declaring it, like this:
public static String LOGIN_REQUEST_URL = "http://suvalkijossapalas.devpanda.eu/checkQr.php";
public Map<String, String> params = new Map<String, String>();
Can anyone shed some light on this for me? I'm buiding this app for my final project that's due on Friday. Head. is. FRIED.
Getting this error on build
C:\Users\Gary\AndroidStudioProjects\NatureAll.v2\app\src\main\java\com\example\gary\natureallv2\SearchAnimalActivity.java:58: error: incompatible types: int cannot be converted to String
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(POST,
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
It's telling me int can't be converted to string but I can't see where the int is. Here's my code:
package com.example.gary.natureallv2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by Gary on 12/09/2016.Go Me
*/
public class SearchAnimalActivity extends AppCompatActivity{
EditText etSearchName;
Button btnSearch;
RequestQueue requestQueue;
String showUrl = "http://192.168.1.10/myDocs/mainProject/search_animal_and.php";
// String showUrl = "http://192.168.1.10/tutorial/showStudents.php";
TextView tvName;
TextView tvLatinName;
TextView tvDescription;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_animal_activity);
etSearchName = (EditText) findViewById(R.id.etSearchName);
tvName = (TextView) findViewById(R.id.tvName);
tvLatinName = (TextView) findViewById(R.id.tvLatinName);
tvDescription = (TextView) findViewById(R.id.tvDescription);
btnSearch = (Button) findViewById(R.id.btnSearch);
requestQueue = Volley.newRequestQueue(getApplicationContext());
btnSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// System.out.println("ww");
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
showUrl, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
System.out.println(response.toString());
try {
JSONArray animals = response.getJSONArray("animals");
for (int i = 0; i < animals.length(); i++) {
JSONObject animal = animals.getJSONObject(i);
//Change here for different string names
String name = animal.getString("name");
String latinName = animal.getString("latinName");
String description = animal.getString("description");
tvName.append(name);
tvLatinName.append(latinName);
tvDescription.append(description);
}
//result.append("===\n");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.append(error.getMessage());
}
});
requestQueue.add(jsonObjectRequest);
}
});
}
}
Many thanks in advance
tvName.append(name);
tvLatinName.append(latinName);
tvDescription.append(description);
You are trying to append a string to a View which has a value of its id (int). This wont work. you should instead call a .setText() on your view and set the text to its current value plus what you want to append.