0
votes

I'm trying to "open" a webpage with this code:

    public HttpResponse hitUrl(String url) {
      try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(new HttpGet(url));
        return response;
      } catch (Exception e) {
          e.printStackTrace();
        return null;
      }
    }

and call the method with

hitUrl("http://www.test.com/Example.php");

My PHP code is:

<?php
$userdatei = fopen ("example.txt","r");
$zeile = fgets($userdatei, 500);
$zeile++;
fclose($userdatei);

$schreiben = fopen ("example.txt","w");
fwrite($schreiben, $zeile);
fclose($schreiben);

echo $zeile;
?>

But the PHP is not counting. If I open the PHP with the Browser it does count! But with this code not. What's wrong with it? And yes we're using the internet permission in the Manifest

edit: Here the full code:

package com.example.phpcheck;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b1=(Button)findViewById(R.id.button1);



    b1.setOnClickListener(new View.OnClickListener() { //Neu Button
       public void onClick(View view) {
        hitUrl("http://www.test.com/Example.php");
       }
    }); 
}

public HttpResponse hitUrl(String url) {
    try {
      HttpClient httpclient = new DefaultHttpClient();
      HttpResponse response = httpclient.execute(new HttpGet(url));
      return response;
    } catch (Exception e) {
        e.printStackTrace();
      return null;
    }
  }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

and here the full logcat:

03-12 17:52:11.942: W/ActivityThread(21149): Application com.example.phpcheck is waiting for the debugger on port 8100... 03-12 17:52:52.926: W/System.err(21149): android.os.NetworkOnMainThreadException 03-12 17:52:52.950: W/System.err(21149): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 03-12 17:52:52.950: W/System.err(21149): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 03-12 17:52:52.950: W/System.err(21149): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 03-12 17:52:52.950: W/System.err(21149): at java.net.InetAddress.getAllByName(InetAddress.java:214) 03-12 17:52:52.950: W/System.err(21149): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 03-12 17:52:52.958: W/System.err(21149): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 03-12 17:52:52.958: W/System.err(21149): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 03-12 17:52:52.958: W/System.err(21149): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 03-12 17:52:52.958: W/System.err(21149): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 03-12 17:52:52.958: W/System.err(21149): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 03-12 17:52:52.965: W/System.err(21149): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 03-12 17:52:52.965: W/System.err(21149): at com.example.phpcheck.MainActivity.hitUrl(MainActivity.java:35) 03-12 17:52:52.965: W/System.err(21149): at com.example.phpcheck.MainActivity$1.onClick(MainActivity.java:27) 03-12 17:52:52.965: W/System.err(21149): at android.view.View.performClick(View.java:4204) 03-12 17:52:52.965: W/System.err(21149): at android.view.View$PerformClick.run(View.java:17355) 03-12 17:52:52.965: W/System.err(21149): at android.os.Handler.handleCallback(Handler.java:725) 03-12 17:52:52.965: W/System.err(21149): at android.os.Handler.dispatchMessage(Handler.java:92) 03-12 17:52:52.973: W/System.err(21149): at android.os.Looper.loop(Looper.java:137) 03-12 17:52:52.973: W/System.err(21149): at android.app.ActivityThread.main(ActivityThread.java:5226) 03-12 17:52:52.973: W/System.err(21149): at java.lang.reflect.Method.invokeNative(Native Method) 03-12 17:52:52.973: W/System.err(21149): at java.lang.reflect.Method.invoke(Method.java:511) 03-12 17:52:52.973: W/System.err(21149): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) 03-12 17:52:52.973: W/System.err(21149): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 03-12 17:52:52.981: W/System.err(21149): at dalvik.system.NativeStart.main(Native Method) 03-12 17:54:18.692: W/System.err(21931): android.os.NetworkOnMainThreadException 03-12 17:54:18.692: W/System.err(21931): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 03-12 17:54:18.692: W/System.err(21931): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 03-12 17:54:18.692: W/System.err(21931): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 03-12 17:54:18.692: W/System.err(21931): at java.net.InetAddress.getAllByName(InetAddress.java:214) 03-12 17:54:18.692: W/System.err(21931): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 03-12 17:54:18.700: W/System.err(21931): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 03-12 17:54:18.700: W/System.err(21931): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 03-12 17:54:18.700: W/System.err(21931): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 03-12 17:54:18.700: W/System.err(21931): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 03-12 17:54:18.700: W/System.err(21931): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 03-12 17:54:18.700: W/System.err(21931): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 03-12 17:54:18.700: W/System.err(21931): at com.example.phpcheck.MainActivity.hitUrl(MainActivity.java:35) 03-12 17:54:18.700: W/System.err(21931): at com.example.phpcheck.MainActivity$1.onClick(MainActivity.java:27) 03-12 17:54:18.700: W/System.err(21931): at android.view.View.performClick(View.java:4204) 03-12 17:54:18.700: W/System.err(21931): at android.view.View$PerformClick.run(View.java:17355) 03-12 17:54:18.700: W/System.err(21931): at android.os.Handler.handleCallback(Handler.java:725) 03-12 17:54:18.700: W/System.err(21931): at android.os.Handler.dispatchMessage(Handler.java:92) 03-12 17:54:18.700: W/System.err(21931): at android.os.Looper.loop(Looper.java:137) 03-12 17:54:18.700: W/System.err(21931): at android.app.ActivityThread.main(ActivityThread.java:5226) 03-12 17:54:18.700: W/System.err(21931): at java.lang.reflect.Method.invokeNative(Native Method) 03-12 17:54:18.700: W/System.err(21931): at java.lang.reflect.Method.invoke(Method.java:511) 03-12 17:54:18.700: W/System.err(21931): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) 03-12 17:54:18.700: W/System.err(21931): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 03-12 17:54:18.700: W/System.err(21931): at dalvik.system.NativeStart.main(Native Method)

1
Have you checked the logs for errors? What gets returned by response?MrChaz
@MrChaz How to check this?pesc
Take a look in logcat or use a breakpoint in the codeMrChaz
@MrChaz:Got this: 03-11 22:46:29.654 W/System.err(15367): at com.example.phpcheck.MainActivity.hitUrl(MainActivity.java:35) 03-11 22:46:29.654 W/System.err(15367): at com.example.phpcheck.MainActivity$1.onClick(MainActivity.java:27)pesc
OK so there's an error in there. Can you post the full log - it will most likely give a more exact error in there. The full source of the activity could be useful tooMrChaz

1 Answers

0
votes

Got it:

Just add this lines of code before the "hitURL"

StrictMode.ThreadPolicy policy = new   
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

Thank for all the help :)