我想在我的Android应用中获取我网站的数据。我尝试使用以下代码,应用程序启动,然后显示简单的activity_main.xml,并在屏幕上加载数据。我附上我的代码,请指导我。
MainActivity.java:
package com.example.httpexample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity
{
TextView httpStuff;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
httpStuff = (TextView) findViewById(R.id.tvHttp);
GetMethodEx test = new GetMethodEx();
String returned;
try
{
returned = test.getInternetData();
httpStuff.setText(returned);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
GetMethodEx.java:
package com.example.httpexample;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class GetMethodEx
{
public String getInternetData() throws Exception
{
BufferedReader in = null;
String data = null;
try
{
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://www.hashmedia.in");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) != null)
{
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
}
finally
{
if(in!=null)
{
try
{
in.close();
return data;
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}
activity_main.xml中:
<LinearLayout 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.httpexample.MainActivity" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/tvHttp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading Data" />
</ScrollView>
</LinearLayout>
答案 0 :(得分:0)
通过意图进行简单的网站浏览
Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://www.hashmedia.in"));
startActivity(viewIntent);
使用这个简单的代码在Android应用程序中查看您的网站。
如果您想获取特定数据,请使用使用JSON的Web服务。
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
在清单文件中添加互联网权限
<
uses-permission android:name="android.permission.INTERNET" />
答案 1 :(得分:0)
空白屏幕?您是否已将INTERNET权限定义到清单文件中?
<uses-permission android:name="android.permission.INTERNET" />
答案 2 :(得分:0)
尝试这样的事情:
<强> youlayout.xml 强>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<强> yourcode.java 强>
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.hashmedia.in");
<强>的manifest.xml 强>
<manifest ... >
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>