Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从internet获取文本_Java_Android_Text - Fatal编程技术网

Java 从internet获取文本

Java 从internet获取文本,java,android,text,Java,Android,Text,如果你去-你可以看到它给ip。如果刷新,它将提供不同的端口 我怎样才能将IP接入android?我不知道如何让它每5秒钟更新一次,但现在我想知道如何把它放到我的手机里。我想将其显示为文本视图:)。编辑:固定代码 尝试一个(示例的简化版本): @拖地雪橇解决方案对我不起作用,因此我的解决方案如下: public class TestActivity extends Activity { /** Called when the activity is first created. */ @Over

如果你去-你可以看到它给ip。如果刷新,它将提供不同的端口


我怎样才能将IP接入android?我不知道如何让它每5秒钟更新一次,但现在我想知道如何把它放到我的手机里。我想将其显示为文本视图:)。

编辑:固定代码

尝试一个(示例的简化版本):


@拖地雪橇解决方案对我不起作用,因此我的解决方案如下:

public class TestActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView tv = (TextView) findViewById(R.id.textView1);
    String ip = "";
    final DefaultHttpClient httpClient = new DefaultHttpClient();
    final HttpGet httpGet = new HttpGet("http://www.elven.ee/ip/");
    try {
        final HttpResponse response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            ip = getString(response);
        }
    } catch (final ClientProtocolException e) {
        e.printStackTrace();
    } catch (final IOException e) {
        e.printStackTrace();
    }
    tv.setText(ip);
}

private static String getString(HttpResponse response) {
    final HttpEntity retEntity = response.getEntity();
    if (retEntity != null) {
        InputStream instream = null;
        try {
            instream = retEntity.getContent();
        } catch (final IllegalStateException ise) {
            ise.printStackTrace();
        } catch (final IOException ioe) {
            ioe.printStackTrace();
        }
        final String result = convertStreamToString(instream);
        return result;
    } else {
        return "";
    }
}

private static String convertStreamToString(final InputStream is) {
    final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    final StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (final IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (final IOException ioe) {
            ioe.printStackTrace();
        }
    }
    return sb.toString().trim();
}
}

别忘了将
添加到您的清单文件中。哇,它成功了!非常感谢:)!我现在需要正确地检查它来理解它:D!你知道如何将其设置为每5秒刷新一次吗:/?@Elven你可以在android开发者资源中找到一个关于计时器的好教程:它给出错误未处理的异常类型MalformedURLException和IOException:/@Elven我知道你已经选择了正确的答案,但是我已经更正了代码,所以现在应该可以正常工作了。好的,谢谢:)。我也要试试。我也可以通过它来测试和学习!这对我来说总是一样的IP。仅限
后面的数字每次呼叫都会更改,但这不是IP的一部分(可能是端口号?)哦,是的,这就是我的意思,端口会更改:)。我的错误。现在我只需要找出我他妈的怎么做循环:(。
public class TestActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView tv = (TextView) findViewById(R.id.textView1);
    String ip = "";
    final DefaultHttpClient httpClient = new DefaultHttpClient();
    final HttpGet httpGet = new HttpGet("http://www.elven.ee/ip/");
    try {
        final HttpResponse response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            ip = getString(response);
        }
    } catch (final ClientProtocolException e) {
        e.printStackTrace();
    } catch (final IOException e) {
        e.printStackTrace();
    }
    tv.setText(ip);
}

private static String getString(HttpResponse response) {
    final HttpEntity retEntity = response.getEntity();
    if (retEntity != null) {
        InputStream instream = null;
        try {
            instream = retEntity.getContent();
        } catch (final IllegalStateException ise) {
            ise.printStackTrace();
        } catch (final IOException ioe) {
            ioe.printStackTrace();
        }
        final String result = convertStreamToString(instream);
        return result;
    } else {
        return "";
    }
}

private static String convertStreamToString(final InputStream is) {
    final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    final StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (final IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (final IOException ioe) {
            ioe.printStackTrace();
        }
    }
    return sb.toString().trim();
}
}