简体   繁体   English

Android:Internet可以在模拟器中运行,但不能在我的手机上运行

[英]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.... 因此,您可以执行以下操作...。

  1. 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保留创建它的线程的引用。

  2. 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.

相关问题 Android:Mediaplayer 可以在模拟器上运行,但不能在手机上运行 - Android: Mediaplayer works in emulator but not on phone Android语音到文本可以在模拟器中运行,但不能在手机上运行 - Android speech to text works in emulator but not on phone Android:如何将我的Android模拟器连接到互联网? - Android: How to connect my android emulator to the internet? Android上的WebView App无法访问互联网,而模拟器工作正常 - WebView App on android not able to access internet while emulator works fine android-android studio模拟器中的SSL问题,可以在手机上正常工作 - android - SSL problems in android studio emulator, works fine on phone Android应用程序可以在真实手机上运行,​​但不能在模拟器上运行-Android开发吗? - Android app works on real phone but not on the emulator - Android Development? GPS Android-无法在实际手机上检索位置,但可以在模拟器上使用 - GPS Android - Cannot retrieve location on actual phone but works on emulator Android:清除并更新手势上的Webview可以在模拟器上而不是手机上运行 - Android: Clear and update webview on gesture works in emulator not on phone Android Studio-具有图像的活动会使模拟器崩溃,但可在手机中使用 - Android Studio - Activity with image crashes emulator but works in phone Android:应用程序在模拟器上运行,但在我的手机上崩溃 - Android: App runs on emulator but it crashes on my mobile phone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM