Я пытаюсь "открыть" веб-страницу с этим кодом:HTTP-GET с Android
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;
}
}
и вызвать метод с
hitUrl("http://www.test.com/Example.php");
Мой PHP код:
<?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;
?>
Но PHP не учитывает. Если я открою PHP с помощью браузера, он посчитает! Но с этим кодом нет. Что с этим не так? И да мы используем разрешение Интернет в Manifest
редактирования: Вот полный код:
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;
}
}
и здесь полный LogCat:
03- 12 17: 52: 11.942: W/ActivityThread (21149): Приложение com.example.phpcheck ожидает отладчика на порту 8100 ... 03-12 17: 52: 52.926: W/System.e р-р (21149): android.os.NetworkOnMainThreadException 03-12 17: 52: 52,950: Вт/System.err (21149): в 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 .ru (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): в 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 (родной метод) 03-12 17: 52: 52.973: W/System.err (21149): at java.lang. reflection.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): при com.android.internal.os.ZygoteInit.main (ZygoteInit.java:562) 03-12 17: 52: 52.981: W/System.err (21149): at dalvik.system.NativeStart.main (собственный метод) 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.InetAd dress.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.AbstractHttpCl ient.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): a t 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 (родной метод) 03-12 17:54: 18.700: W/System.err (21931): at java.lang.reflection.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): в dalvik.system.NativeStart.main (Native Method)
Вы проверили журналы на наличие ошибок? Что возвращается ответ? – MrChaz
@MrChaz Как это проверить? – pesc
Посмотрите в logcat или используйте контрольную точку в коде – MrChaz