I try to connect with WiFi module, but i have unfortunately error. It is in 102 line that is:
HttpResponse response = httpclient.execute(getRequest); // execute the request
Here is my all code:
public class WiFi extends ListActivity implements View.OnClickListener {
public final static String PREF_IP = WiFiComuncation.PREF_IP;
public final static String PREF_PORT = WiFiComuncation.PREF_PORT;
SharedPreferences.Editor editor;
SharedPreferences sharedPreferences;
public static final String RESPONSE = "Response";
private String serverResponse = "ERROR"; // Ustawiam na domyslny blad problemu z polaczeniem z modułem WiFI
private String ipAddress, portNumber;
private String parameterValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ipAddress=PREF_IP;
portNumber=PREF_PORT;
sharedPreferences = getSharedPreferences("HTTP_HELPER_PREFS", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putString(PREF_IP, "192.168.4.1"); // set the ip address value to save
editor.putString(PREF_PORT, "80"); // set the port number to save
editor.commit(); // save the IP and PORT
serverResponse = sendRequest(parameterValue,ipAddress,portNumber); // wysyłam zapytanie do mikrokloca
Intent intent = getIntent();
parameterValue = intent.getStringExtra("parameterValue");
checkAnswer(serverResponse);
prepareResponse();
finish();
}
private void prepareResponse() { // Zwracam odpowiedz
String response = serverResponse;
Intent resultIntent = new Intent();
resultIntent.putExtra(RESPONSE, response);
setResult(RESULT_OK, resultIntent);
}
private boolean checkAnswer( String response )
{
if(response.equals("E001")) // problem z polaczeniem z WiFi
{
Toast.makeText(getApplicationContext(), "#E001 - Błąd połączenia urządzenia z modułem WiFi! ", Toast.LENGTH_LONG).show(); // Wyswitlanie komunikatu na apce
return false;
}
if(response.equals("E002")) // Błędny odcisk palca
{
Toast.makeText(getApplicationContext(), "#E002 - Błędny odcisk palca! ", Toast.LENGTH_LONG).show(); // Wyswitlanie komunikatu na apce
return false;
}
if(response.equals("E003")) //Błąd komunikacji pomiędzy podzespołami
{
Toast.makeText(getApplicationContext(), "#E001 - Błąd komunikacji pomiędzy podzespołami! ", Toast.LENGTH_LONG).show(); // Wyswitlanie komunikatu na apce
return false;
}
else
{
return true;
}
}
/**
* Description: Send an HTTP Get request to a specified ip address and port.
* Also send a parameter "parameterName" with the value of "parameterValue".
* @param parameterValue the pin number to toggle
* @param ipAddress the ip address to send the request to
* @param portNumber the port number of the ip address
* @return The ip address' reply text, or an ERROR message is it fails to receive one
*/
public String sendRequest(String parameterValue, String ipAddress, String portNumber) {
try {
HttpClient httpclient = new DefaultHttpClient(); // create an HTTP client
// define the URL e.g. http://myIpaddress:myport/?pin=13 (to toggle pin 13 for example)
URI website = new URI("http://"+ipAddress+":"+portNumber+"/?"+"pin"+"="+"11");
HttpGet getRequest = new HttpGet(website); // create an HTTP GET object
getRequest.setURI(website); // set the URL of the GET request
HttpResponse response = httpclient.execute(getRequest); // execute the request
// get the ip address server's reply
InputStream content = null;
content = response.getEntity().getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(
content
));
serverResponse = in.readLine();
// Close the connection
content.close();
} catch (ClientProtocolException e) {
// HTTP error
serverResponse = e.getMessage();
e.printStackTrace();
} catch (IOException e) {
// IO error
serverResponse = e.getMessage();
e.printStackTrace();
} catch (URISyntaxException e) {
// URL syntax error
serverResponse = e.getMessage();
e.printStackTrace();
}
// return the server's reply/response text
return serverResponse;
}
@Override
public void onClick(View v) {
}
}
And there is LogCat:
> 04-11 14:51:24.399 7540-7540/com.example.iorwet.thoghtaplication
> E/AndroidRuntime: FATAL EXCEPTION: main
> Process: com.example.iorwet.thoghtaplication, PID: 7540
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.example.iorwet.thoghtaplication/com.example.iorwet.thoghtaplication.WiFi}:
> android.os.NetworkOnMainThreadException
> at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
> at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
> at android.app.ActivityThread.access$800(ActivityThread.java:135)
> at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
> at android.os.Handler.dispatchMessage(Handler.java:102)
> at android.os.Looper.loop(Looper.java:136)
> at android.app.ActivityThread.main(ActivityThread.java:5001)
> at java.lang.reflect.Method.invokeNative(Native Method)
> at java.lang.reflect.Method.invoke(Method.java:515)
> at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
> at dalvik.system.NativeStart.main(Native Method)
> Caused by: android.os.NetworkOnMainThreadException
> at
> android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
> at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
> at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
> at libcore.io.IoBridge.connect(IoBridge.java:112)
> at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
> at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
> at java.net.Socket.connect(Socket.java:843)
> at
> org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
> at
> org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
> at
> org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
> at
> org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
> at
> org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
> at
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
> at
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
> at
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
> at com.example.iorwet.thoghtaplication.WiFi.sendRequest(WiFi.java:102)
> at com.example.iorwet.thoghtaplication.WiFi.onCreate(WiFi.java:47)
> at android.app.Activity.performCreate(Activity.java:5231)
> at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
> at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
> at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
> at android.app.ActivityThread.access$800(ActivityThread.java:135)
> at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
> at android.os.Handler.dispatchMessage(Handler.java:102)
> at android.os.Looper.loop(Looper.java:136)
> at android.app.ActivityThread.main(ActivityThread.java:5001)
> at java.lang.reflect.Method.invokeNative(Native Method)
> at java.lang.reflect.Method.invoke(Method.java:515)
> at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
> at dalvik.system.NativeStart.main(Native Method)