Android - 获取网页内容为String
Android - Get webpage content into a String
我正在开发一个新应用程序,它需要从我在 url http://10.0.0.8/mybiren/json.php 中的网站获取一个 JSON 对象(我知道它是一个本地 ip,它仅用于测试应用程序)。
我有获取站点内容的功能,它在常规 Java 项目上工作,但是当我在我的 Android 应用程序项目上尝试它时,它失败了。当我调试应用程序以找到它停止的位置时,它已停止在调用 yc.getInputStream()
的行上(yc 是一个 URLConnection 对象)。我的朋友说我必须将 <uses-permission android:name="android.permission.INTERNET"/>
添加到我的 AndroidManifest.xml 但它没有帮助。
Stack Trace 没有显示任何特别的东西
Java代码
package com.example.arghh_mybiren;
import android.support.v7.app.ActionBarActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void getJson(View v)
{
URL u;
try {
u = new URL("http://10.0.0.8/mybiren/json.php");
URLConnection yc = u.openConnection();
yc.setDoInput(true);
yc.setDoOutput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream())); //debugger crashed here
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
Toast.makeText(getApplicationContext(), a.toString(), Toast.LENGTH_SHORT).show();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
XML代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.arghh_mybiren.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="getJson"
android:onClick="getJson" />
</RelativeLayout>
堆栈跟踪
11-04 19:59:26.452: I/PGA(17170): New SOCKET connection: com.example.mybiren (pid 17170, tid 17170)
11-04 19:59:26.452: W/PGA(17170): [17170] egl: eglCreateWindowSurface (0x5579a7a0, 0x0, 0x787ae3b8, 0x775840e0)
11-04 19:59:26.462: W/PGA(17170): [17170] egl: eglCreateWindowSurface (0x5579a7a0, 0x0, 0x787ae3b8, 0x775840e0) returned
11-04 19:59:26.472: D/OpenGLRenderer(17170): Enabling debug mode 0
我真的不知道出了什么问题,但我的朋友成功地帮助了我并向我发送了以下代码(对我有用):
Java代码
package com.example.arghh_mybiren;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private class MyTask extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String ... urls)
{
try
{
URL url = new URL(urls[0]);
URLConnection uc = url.openConnection();
//String j = (String) uc.getContent();
uc.setDoInput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
return a.toString();
}
catch (Exception e)
{
return e.getMessage();
}
}
@Override
protected void onPostExecute(String result)
{
Toast.makeText(getApplication(), result, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button loadBtn = (Button) this.findViewById(R.id.btn_load);
loadBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Load();
}
});
}
public void Load()
{
Toast.makeText(this, "Button Clicked", Toast.LENGTH_SHORT).show();
MyTask taskLoad = new MyTask();
taskLoad.execute("http://10.0.0.8/mybiren/JSON.php?day=Monday");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.arghh_mybiren.MainActivity" >
<Button
android:id="@+id/btn_load"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="getJson"
android:onClick="getJson" />
</RelativeLayout>
感谢所有帮助过我的人。
我正在开发一个新应用程序,它需要从我在 url http://10.0.0.8/mybiren/json.php 中的网站获取一个 JSON 对象(我知道它是一个本地 ip,它仅用于测试应用程序)。
我有获取站点内容的功能,它在常规 Java 项目上工作,但是当我在我的 Android 应用程序项目上尝试它时,它失败了。当我调试应用程序以找到它停止的位置时,它已停止在调用 yc.getInputStream()
的行上(yc 是一个 URLConnection 对象)。我的朋友说我必须将 <uses-permission android:name="android.permission.INTERNET"/>
添加到我的 AndroidManifest.xml 但它没有帮助。
Stack Trace 没有显示任何特别的东西
Java代码
package com.example.arghh_mybiren;
import android.support.v7.app.ActionBarActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void getJson(View v)
{
URL u;
try {
u = new URL("http://10.0.0.8/mybiren/json.php");
URLConnection yc = u.openConnection();
yc.setDoInput(true);
yc.setDoOutput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream())); //debugger crashed here
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
Toast.makeText(getApplicationContext(), a.toString(), Toast.LENGTH_SHORT).show();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
XML代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.arghh_mybiren.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="getJson"
android:onClick="getJson" />
</RelativeLayout>
堆栈跟踪
11-04 19:59:26.452: I/PGA(17170): New SOCKET connection: com.example.mybiren (pid 17170, tid 17170)
11-04 19:59:26.452: W/PGA(17170): [17170] egl: eglCreateWindowSurface (0x5579a7a0, 0x0, 0x787ae3b8, 0x775840e0)
11-04 19:59:26.462: W/PGA(17170): [17170] egl: eglCreateWindowSurface (0x5579a7a0, 0x0, 0x787ae3b8, 0x775840e0) returned
11-04 19:59:26.472: D/OpenGLRenderer(17170): Enabling debug mode 0
我真的不知道出了什么问题,但我的朋友成功地帮助了我并向我发送了以下代码(对我有用):
Java代码
package com.example.arghh_mybiren;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private class MyTask extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String ... urls)
{
try
{
URL url = new URL(urls[0]);
URLConnection uc = url.openConnection();
//String j = (String) uc.getContent();
uc.setDoInput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
return a.toString();
}
catch (Exception e)
{
return e.getMessage();
}
}
@Override
protected void onPostExecute(String result)
{
Toast.makeText(getApplication(), result, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button loadBtn = (Button) this.findViewById(R.id.btn_load);
loadBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Load();
}
});
}
public void Load()
{
Toast.makeText(this, "Button Clicked", Toast.LENGTH_SHORT).show();
MyTask taskLoad = new MyTask();
taskLoad.execute("http://10.0.0.8/mybiren/JSON.php?day=Monday");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.arghh_mybiren.MainActivity" >
<Button
android:id="@+id/btn_load"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="getJson"
android:onClick="getJson" />
</RelativeLayout>
感谢所有帮助过我的人。