java - url.openConnection(); is not actually invoked -
im building graduation project collage "smart homes, home automation system" implemented arduion on mock-up structure.
to have full image, arduaio takes pin number via request switch on or of specific home device.
its cool when send http request browser, when use
openconnection();
method, it's never happens, when use data home rooms , devices it's working greatly.
i gave app permission access internet.
the code simple project made solve praticualy problem:
mainactivity.java
package com.bitsandbytes.xemma_pc.newprototype; import android.os.asynctask; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.button; import java.io.ioexception; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button button = (button) findviewbyid(r.id.button); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { new sendrequest().execute(); } }); } private static class sendrequest extends asynctask <void,void,void>{ @override protected void doinbackground(void... params) { httpurlconnection httpurlconnection = null ; url url ; try { url = new url("http://192.168.1.143/pin=13"); httpurlconnection = (httpurlconnection) url.openconnection(); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }finally { httpurlconnection.disconnect(); } return null; } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.bitsandbytes.xemma_pc.newprototype.mainactivity"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="press test it" android:id="@+id/textview" android:layout_margintop="154dp" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="pin 13" android:id="@+id/button" android:layout_below="@+id/textview" android:layout_centerhorizontal="true" /> </relativelayout>
androidmanifist.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.bitsandbytes.xemma_pc.newprototype"> <uses-permission android:name="android.permission.internet"/> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
i appreciate , tips in advance.
edit: guys helping me, helped me using getresponsemessage() method, , works :d didn't have use .connect method json strings before, working. anyway, help!
openconnection
parses url , creates appropriate urlconnection
subclass. network i/o doesn't happen until send or receive something.
Comments
Post a Comment