I have the following method that gives me a String
:
public static final String jsonClientes =
" {\"clientes\": " +
" [" +
" { \"idClientesT\": 1," +
" \"tipo\":\"s\"," +
" \"nome\":\"Carlos\"" +
" }," +
" { \"idClientesT\": 2," +
" \"tipo\":\"s\"," +
" \"nome\":\"Rogério\"" +
" }" +
" ]" +
"}";
And the following method to read the String
above:
public void clientes(String jsonCli) {
try {
JSONObject clientes = new JSONObject(jsonCli);
JSONArray arrayPlanetas = clientes.getJSONArray("clientes");
for (int i = 0; i < arrayPlanetas.length(); i++) {
JSONObject cliente = arrayPlanetas.getJSONObject(i);
Log.d("idClientesT", cliente.getString("idClientesT"));
Log.d("tipo", cliente.getString("tipo"));
Log.d("nome", cliente.getString("nome"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
The onCreate
looks like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
clientes(jsonClientes);
}
In this way, I can access the data normally .
Well, I have this URL that reads an exactly return equal to:
{"clientes":[{"idClientesT":"1","tipo":"s","nome":"Carlos"},{"idClientesT":"2","tipo":"s","nome":"Rogério"}]}
But without the proper *scapes*
.
I would like to know how in:
JSONArray arrayPlanetas = clientes.getJSONArray("clientes");
Instead of doing:
.getJSONArray("clientes");
could get the URL return like this:
.getJSONArray(url);
Is this possible?
How to do it?
In an attempt (unsuccessful) I did so:
package carcleo.com.radiosingular;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import carcleo.com.radiosingular.classes.JsonClass;
public class form extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
String url = "http://hotplateprensas.com.br/ws/clientest.php";
JSONObject clientesLista = null;
JsonClass json = new JsonClass();
clientesLista = json.getJSONFromUrl(url);
clientes(clientesLista);
}
public void clientes(JSONObject jsonCli) {
try {
JSONArray arrayPlanetas = jsonCli.getJSONArray("clientes");
for (int i = 0; i < arrayPlanetas.length(); i++) {
JSONObject cliente = arrayPlanetas.getJSONObject(i);
Log.d("idClientesT", cliente.getString("idClientesT"));
Log.d("tipo", cliente.getString("tipo"));
Log.d("nome", cliente.getString("nome"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
and the class JsonClass
package carcleo.com.radiosingular.classes;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import cz.msebera.android.httpclient.HttpEntity;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.client.ClientProtocolException;
import cz.msebera.android.httpclient.client.methods.HttpPost;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
public class JsonClass {
InputStream input = null;
JSONObject jObect = null;
String json = "";
//Recebe sua url
public JSONObject getJSONFromUrl(String url) {
//HTTP request
try {
// default HttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
input = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
input, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
input.close();
json = sb.toString();
Log.i("JRF", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// Transforma a String de resposta em um JSonObject
try {
jObect = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// retorna o objeto
return jObect;
}
}
This gives me the following error:
12/14 14:25:05: Launching app
$ adb install-multiple -r -t -p carcleo.com.radiosingular D:\Trabalhos\AndroidStudio\RadioSingular\app\build\intermediates\split-apk\debug\slices\slice_8.apk
Split APKs installed in 1 s 53 ms
$ adb shell am start -n "carcleo.com.radiosingular/carcleo.com.radiosingular.login" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Connected to process 13274 on device motorola-moto_g_play-0027377835
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/System: ClassLoader referenced unknown path: /data/app/carcleo.com.radiosingular-2/lib/arm
I/InstantRun: starting instant run server: is main process
I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8916_32_LA.BR.1.2.9_RB1__release_AU (I750f7f2fa6)
OpenGL ES Shader Compiler Version: XE031.09.00.03
Build Date: 02/18/17 Sat
Local Branch:
Remote Branch: quic/LA.BR.1.2.9_rb1.12
Local Patches: NONE
Reconstruct Branch: NOTHING
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/Adreno-ES20: <get_gpu_clk:229>: open failed: errno 13
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
I/art: at void android.support.v4.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2341)
at void android.support.v7.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:62)
at java.lang.Object java.lang.reflect.Constructor.newInstance0!(java.lang.Object[]) (Constructor.java:-2)
at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:430)
at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:645)
at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:787)
at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:727)
at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:858)
at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:821)
at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:518)
at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:426)
at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:377)
at android.view.ViewGroup android.support.v7.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:607)
at void android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518)
at void android.support.v7.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466)
at void android.support.v7.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140)
at void carcleo.com.radiosingular.form.onCreate(android.os.Bundle) (form.java:18)
at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6687)
at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1140)
at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2631)
at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2743)
at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1490)
at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
at void android.os.Looper.loop() (Looper.java:154)
at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6165)
at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:888)
at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:778)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/carcleo.com.radiosingular-2/base.apk", zip file "/data/app/carcleo.com.radiosingular-2/split_lib_dependencies_apk.apk", zip file "/data/app/carcleo.com.radiosingular-2/split_lib_slice_0_apk.apk", zip file "/data/app/carcleo.com.radiosingular-2/split_lib_slice_1_apk.apk", zip file "/data/app/carcleo.com.radiosingular-2/split_lib_slice_2_apk.apk", zip file "/data/app/carcleo.com.radiosingular-2/split_lib_slice_3_apk.apk", zip file "/data/app/carcleo.com.radiosingular-2/split_lib_slice_4_apk.apk", zip file "/data/app/carcleo.com.radiosingular-2/split_lib_slice_5_apk.apk", zip file "/data/app/carcleo.com.radiosingular-2/split_lib_slice_6_apk.apk", zip file "/data/app/carcleo.com.radiosingular-2/split_lib_slice_7_apk.apk", zip file "/data/app/carcleo.com.radiosingular-2/split_lib_slice_8_apk.apk", zip file "/data/app/carcleo.com.radiosingular-2/split_lib_slice_9_
at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:380)
at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
at void android.support.v4.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2341)
at void android.support.v7.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:62)
at java.lang.Object java.lang.reflect.Constructor.newInstance0!(java.lang.Object[]) (Constructor.java:-2)
at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:430)
at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:645)
at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:787)
at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:727)
at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:858)
at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:821)
at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:518)
at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:426)
at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:377)
at android.view.ViewGroup android.support.v7.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:607)
at void android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518)
at void android.support.v7.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466)
at void android.support.v7.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140)
at void carcleo.com.radiosingular.form.onCreate(android.os.Bundle) (form.java:18)
at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6687)
at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1140)
at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2631)
at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2743)
at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1490)
at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
at void android.os.Looper.loop() (Looper.java:154)
at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6165)
at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:888)
at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:778)
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: carcleo.com.radiosingular, PID: 13274
java.lang.RuntimeException: Unable to start activity ComponentInfo{carcleo.com.radiosingular/carcleo.com.radiosingular.form}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1317)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
at java.net.InetAddress.getAllByName(InetAddress.java:752)
at cz.msebera.android.httpclient.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:44)
at cz.msebera.android.httpclient.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:259)
at cz.msebera.android.httpclient.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:159)
at cz.msebera.android.httpclient.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
at cz.msebera.android.httpclient.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:860)
at cz.msebera.android.httpclient.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at cz.msebera.android.httpclient.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at carcleo.com.radiosingular.classes.JsonClass.getJSONFromUrl(JsonClass.java:35)
at carcleo.com.radiosingular.form.onCreate(form.java:23)
at android.app.Activity.performCreate(Activity.java:6687)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)