[英]Android: Internet works in emulator but not on my phone
I'm making an app which reads a certain part of a website and posts it to the screen. 我正在制作一个应用程序,它可以读取网站的特定部分并将其发布到屏幕上。 The app currently works in my android emulator but when I transfer it to my galaxy S2, it doesn't seem to access the website at all.
该应用程序当前可在我的Android模拟器中运行,但是当我将其转移到银河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");
}
});
}
} }
Apologies if the coding is a bit poor and messy, I'm quite new to all this. 抱歉,如果编码有点糟糕且凌乱,我对所有这些都是新手。 How do I fix this issue?
如何解决此问题? Thank you
谢谢
Also, I'm pretty sure I've set the permission correctly in the manifest. 另外,我很确定自己已在清单中正确设置了权限。
Try this.... 尝试这个....
Its a good practice to Keep the UI work on UI thread and Non-UI work on Non-UI thread, but that became a LAW from the release of HONEYCOMB Android version .... That may be causing the error. 保持UI在UI线程上工作以及Non-UI在Non-UI线程上工作是一个好习惯,但是从HONEYCOMB Android版本的发行中这已成为法律 ....这可能导致错误。
So you can do the following.... 因此,您可以执行以下操作...。
Do the Http on a separate thread, and then place the value on the UI using Handler , Handler Keep the reference of the thread in which it was created. 在单独的线程上执行Http,然后使用Handler将值放在UI上,Handler保留创建它的线程的引用。
You can use AsyncTask which is specially designed for android to make a sync between UI and Non-UI thread. 您可以使用专为android设计的AsyncTask在UI和Non-UI线程之间进行同步。
Note: 注意:
Please check the Internet Permission in AndroidManifest, though i know you have done it, cause the app ran on the emulator.... but still..no harm in checking it again. 请检查AndroidManifest中的Internet权限,尽管我知道您已经这样做了,但导致该应用程序在模拟器上运行了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.