我正在制作一个应用程序,它可以读取网站的某个部分,并将其发布到屏幕上。这个应用程序目前可以在我的android模拟器上运行,但当我将它传输到我的galaxy S2上时,它似乎根本无法访问该网站。
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");
}
});
}
}
如果代码有点糟糕和凌乱,我很抱歉,我对这一切都很陌生。如何解决此问题?谢谢
另外,我非常确定我已经在清单中正确地设置了权限。
发布于 2012-06-28 20:44:39
试试这个..。
让UI在UI线程上工作,让非UI线程在非UI线程上工作,这是一个很好的做法,但从HONEYCOMB版本的发布开始,这就成了一条法则……这可能是导致错误的原因。
因此,您可以执行以下操作...
注意:
请检查AndroidManifest中的互联网权限,尽管我知道你已经这样做了,因为应用程序是在模拟器上运行的。但still..no在再次检查它时会带来危害。
https://stackoverflow.com/questions/11244224
复制相似问题