2
Following the answer given to the question:
Which is exactly the same problem as mine, I arrived in class:
package com.example.carlos.radiosingular.classes;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpConnections {
//método get
public static String get(String urlString){
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String resposta = null;
try {
URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
StringBuffer buffer = new StringBuffer();
while ((line = reader.readLine()) != null){
buffer.append(line);
}
resposta = buffer.toString();
}catch (Exception e){
e.printStackTrace();
}finally {
if (urlConnection != null){
urlConnection.disconnect();
}
try {
reader.close();
}catch (Exception e){
e.printStackTrace();
}
}
return resposta;
}
}
My classe
leading:
package com.example.carlos.radiosingular;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.example.carlos.radiosingular.classes.HttpConnections;
public class form extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
String resposta = HttpConnections.get("http://hotplateprensas.com.br/ws/clientest.php");
Log.v("Resposta: ", resposta);
}
}
Error in my Moto G 4 Play (I am emulating directly on the device)
RadioSingular parou
Abri App novamente
Console error RUN
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.carlos.radiosingular, PID: 21972
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.carlos.radiosingular/com.example.carlos.radiosingular.form}: java.lang.NullPointerException: println needs a message
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: java.lang.NullPointerException: println needs a message
at android.util.Log.println_native_inner(Native Method)
at android.util.Log.println_native(Log.java:294)
at android.util.Log.v(Log.java:306)
at com.example.carlos.radiosingular.form.onCreate(form.java:17)
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)
Errors in the Logcat
(only the last lines)
2018-12-13 10:36:14.455 22364-22415/? E/GmsUtils: Failed to connect to Google API client: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}
2018-12-13 10:36:29.136 4236-20786/? E/SQLiteCastStore: Skip saving CastDeviceInfo: "Dispositivo próximo" (__cast_nearby___V_d580d25c-abe2-4303-9f7d-361d2bee8a9f)
2018-12-13 10:36:34.251 393-2691/? E/NetlinkEvent: NetlinkEvent::FindParam(): Parameter 'UID' not found
What to do?
The error is in the line:
Log.v("Resposta: ", resposta);
When I comment on it, it makes no mistake.
If answer is a string:
String resposta = HttpConnections.get("http://hotplateprensas.com.br/ws/clientest.php");
and, commenting
//Log.v("Resposta: ", resposta);
The error goes away,
It doesn’t make sense to me once running in the browser, the url http://hotplateprensas.com.br/ws/clientest.php, brings me as a return JSON
correctly:
[
{"idClientesT":"1","tipo":"s","nome":"Carlos"},
{"idClientesT":"2","tipo":"s","nome":"Rogério"}
]
Where is the error?
EDIT:
In the manifest
did so:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
It seems to me that he is not managing to print a JSON. For testing do this:
Log.v("Resposta: ", "teste");
to see if the error occurs.– Reginaldo Rigo
worked well, gave no error and displayed on V/Reply console:: test
– Carlos Rocha
Well, does it have to do with libration for intenet use? In the manifest I did so <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> but still does not run
– Carlos Rocha
Try it like this now
Log.v("Resposta:", resposta.toString());
– Reginaldo Rigo
Caused by: java.lang.Nullpointerexception: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null Object Reference
– Carlos Rocha
looks like he’s not getting the json
– Carlos Rocha
Turns out the process is asynchronous. :
String resposta = HttpConnections.get("http://hotplateprensas.com.br/ws/clientest.php");
and does not wait for the answer and immediately at the bottom line before the call returns you already want to print it, when the answer is still null.– Reginaldo Rigo
um, I think I get it! It takes a really long time. You indicate me some link to research on the subject?
– Carlos Rocha
https://stackoverflow.com/questions/19229204/android-waiting-for-response-from-server
– Reginaldo Rigo