0
Good night!
I made the following code to send data from a Webservice Json to a Textview from a button:
package br.edu.ipesu.thermostop;
import java.io.InputStreamReader;
import java.net.URL;
import com.google.gson.Gson;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
Button botaoTemperatura;
TextView resultadoTemperatura;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
botaoTemperatura = (Button) findViewById(R.id.botaoTemperatura);
botaoTemperatura.setOnClickListener((android.view.View.OnClickListener)this);
resultadoTemperatura = (TextView) findViewById(R.id.resultadoTemperatura);
resultadoTemperatura.setOnClickListener((android.view.View.OnClickListener)this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId()==R.id.botaoTemperatura){
try {
String name = "30%20Graus";
String url = "http://117.193.26.104:8080/WelcomeRESTJSON/" + "resources/welcome/" + name ;
InputStreamReader reader = new InputStreamReader (new URL(url).openStream());
TextMessage message = new Gson().fromJson(reader, TextMessage.class);
resultadoTemperatura.setText(message.getMessage().toString());
}
catch (Exception exception) {
resultadoTemperatura.setText("Erro!");
exception.printStackTrace();
}
}
}
class TextMessage {
private String message;
public String getMessage(){
return message;
}
public void setMessage(String value){
message = value;
}
}
}
The code runs perfectly when tested on Android 2.2, but in versions 4.2 onwards the same jumps to the catch Exception. What would be the problem?
PS: I added Logcat by clicking the button below:
09-27 02:25:37.530: W/System.err(1201): android.os.NetworkOnMainThreadException
09-27 02:25:37.579: W/System.err(1201): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
09-27 02:25:37.579: W/System.err(1201): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
09-27 02:25:37.663: W/System.err(1201): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
09-27 02:25:37.663: W/System.err(1201): at libcore.io.IoBridge.connect(IoBridge.java:112)
09-27 02:25:37.669: W/System.err(1201): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-27 02:25:37.683: W/System.err(1201): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
09-27 02:25:37.683: W/System.err(1201): at java.net.Socket.connect(Socket.java:842)
09-27 02:25:37.689: W/System.err(1201): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
09-27 02:25:37.701: W/System.err(1201): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
09-27 02:25:37.701: W/System.err(1201): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
09-27 02:25:37.701: W/System.err(1201): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
09-27 02:25:37.722: W/System.err(1201): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
09-27 02:25:37.729: W/System.err(1201): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
09-27 02:25:37.750: W/System.err(1201): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
09-27 02:25:37.761: W/System.err(1201): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
09-27 02:25:37.761: W/System.err(1201): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
09-27 02:25:37.761: W/System.err(1201): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
09-27 02:25:37.761: W/System.err(1201): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
09-27 02:25:37.769: W/System.err(1201): at java.net.URL.openStream(URL.java:462)
09-27 02:25:37.800: W/System.err(1201): at br.edu.ipesu.thermostop.MainActivity.onClick(MainActivity.java:41)
09-27 02:25:37.800: W/System.err(1201): at android.view.View.performClick(View.java:4204)
09-27 02:25:37.800: W/System.err(1201): at android.view.View$PerformClick.run(View.java:17355)
09-27 02:25:37.819: W/System.err(1201): at android.os.Handler.handleCallback(Handler.java:725)
09-27 02:25:37.819: W/System.err(1201): at android.os.Handler.dispatchMessage(Handler.java:92)
09-27 02:25:37.819: W/System.err(1201): at android.os.Looper.loop(Looper.java:137)
09-27 02:25:37.849: W/System.err(1201): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-27 02:25:37.849: W/System.err(1201): at java.lang.reflect.Method.invokeNative(Native Method)
09-27 02:25:37.849: W/System.err(1201): at java.lang.reflect.Method.invoke(Method.java:511)
09-27 02:25:37.861: W/System.err(1201): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-27 02:25:37.861: W/System.err(1201): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-27 02:25:37.861: W/System.err(1201): at dalvik.system.NativeStart.main(Native Method)
SOLVED! I used an Asynctask, where the lines of Inputstreamreader and Textmessage were in theInBackground returning null and the setText of Textview was in onPostExecute.
could you post Exception? would help you!
– Thiago Luiz Domacoski
Just out of curiosity, because you add Onclicklistener to Textview?
– Thiago Luiz Domacoski
I implemented Onclicklistener in the Mainactivity class and made onClick(View v) modify Textview by clicking the button. It works because I have already tested Try without the Inputstreamreader line and send any String in Textview setText. By what I see is occurring an exception in Inputstreamreader when it runs on Android 4.2 or higher (it jumps pro catch Exception and runs his setText with the phrase "Error!". I’m searching for another alternative to use Inputstreamreader to load the JSON url but it’s hard. OBS: I am using the Eclipse that comes with ADT 20130729.
– Fábio Adriano
and Exception.printStackTrace(); shows something in the log cat? Do the following: modify the following resultTemperature.setText line("Error!"); TO: resultTemperatura.setText(Exception.getMessage()); and post here to know exactly the error!
– Thiago Luiz Domacoski
Doing this shows nothing on the screen. Here is the logCat when you click the button: http://notepad.cc/share/jcsEJaQpZO
– Fábio Adriano
OSB: Android.permission.INTERNET (Uses Permission) is on Androidmanifest.xml.
– Fábio Adriano