2013-03-11 103 views
0

我試圖 「打開」 網頁使用此代碼: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,它會計數!但是這個代碼沒有。它出什麼問題了? 是的,我們正在使用的清單

編輯Internet權限:在此,全碼:

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):Application com.example.phpcheck正在等待調試器在端口8100上... 03-12 17:52:52.926:W/System.e RR(21149): android.os.NetworkOnMainThreadException 03-12 17:52:52.950: W/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 .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):在 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 W/System.err(21931):17:54:18.692:W/System.err(21931):at java.net.InetAddress.getAllByNameImpl :在 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(Native Method)03-12 17:54: 18.700:W/System.err(21931):位於 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):在 dalvik.system.NativeStart.main(本機方法)

+0

您是否檢查過錯誤日誌?迴應返回什麼? – MrChaz 2013-03-11 21:17:56

+0

@MrChaz如何檢查? – pesc 2013-03-11 21:18:37

+0

看看logcat或在代碼中使用斷點 – MrChaz 2013-03-11 21:19:29

回答

0

明白了:

的 「hitURL」

前只需添加以下代碼行
StrictMode.ThreadPolicy policy = new 
StrictMode.ThreadPolicy.Builder().permitAll().build(); 
StrictMode.setThreadPolicy(policy); 

感謝所有幫助:)

+0

這是一個非常糟糕的黑客攻擊。你有一個NetworkOnMainThreadException。解決方案是將代碼放入線程或AsyncTask中。 – greenapps 2013-03-14 15:23:16

+0

@greenapps爲什麼? – pesc 2013-03-15 23:43:44

+0

因爲谷歌已經引入了這個功能,以便在從互聯網下載期間使應用更加靈敏。互聯網流量不在主線程中是否重要? – greenapps 2013-03-19 16:43:58