2012-06-28 62 views
1

我正在制作一个应用程序,它读取网站的某个部分并将其张贴到屏幕上。该应用程序目前在我的android模拟器中工作,但是当我将它传输到我的galaxy S2时,它似乎根本没有访问该网站。Android:互联网在模拟器中工作,但不在我的手机上

package com.example.beam; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.URL; 
import java.net.URLConnection; 
import java.util.Scanner; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.*; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.BasicResponseHandler; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.protocol.BasicHttpContext; 
import org.apache.http.protocol.HttpContext; 
import org.apache.http.util.EntityUtils; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.support.v4.app.NavUtils; 

public class MainActivity extends Activity { 

String current = null; 
Button check; 
TextView text; 
TextView url; 
String[] lines = new String[12]; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 



    check = (Button) findViewById(R.id.checkstatus); 
    text = (TextView) findViewById(R.id.textView1); 
    url = (TextView) findViewById(R.id.url); 

    String[] lines = new String[12]; 

    check.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      // Attempt to the read the source from a website. 
      String bodyHtml = "null"; 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpGet httpGet = new  HttpGet("http://www.spring8.or.jp/ext/ja/status/text.html"); 
      ResponseHandler<String> resHandler = new BasicResponseHandler(); 

      try { 
       bodyHtml = httpClient.execute(httpGet, resHandler); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      double current = 0; 
      try{ 
      String derp = bodyHtml.substring(bodyHtml.lastIndexOf("mA") - 5, bodyHtml.lastIndexOf("mA")); 
      current = Double.parseDouble(derp); 
      } 
      catch(Exception e){ 

      } 
      url.setText(current + " mA"); 


     } 
    }); 
} 

}

道歉,如果编码是有点差,乱,我很新的这一切。我如何解决这个问题?谢谢

此外,我敢肯定我已经在清单中正确设置权限。

+0

请发布logcat。 –

回答

1

试试这个....

它是一个很好的做法,以保持对UI线程和非UI线程非UI的工作界面的工作,不过成为从蜂巢Android版本的发布一个 ....这可能会导致错误。

所以,你可以做以下....

  1. 执行HTTP运行在一个单独的线程,然后将使用处理器 UI中的值,处理器保持线程的引用它被创建。

  2. 您可以使用的AsyncTask这是专门为Android做 UI和非UI线程之间的同步。

注:

请检查AndroidManifest上网权限,虽然我知道你已经做到了,导致应用程序运行在模拟器上....但在再次检查它still..no危害。

+0

谢谢,我在互联网上找到了一个使用AsyncTask的例子,它工作正常! – user1147964

相关问题