2014-11-06 6 views
1

내 Android 앱에서 내 웹 사이트의 데이터를 가져 오려고합니다. 다음 코드를 사용하여 응용 프로그램을 시작한 다음 간단한 activity_main.xml이 화면에 데이터로드와 함께 표시됩니다. 제 코드를 달고 있습니다. 제발 저를 안내해주십시오.android app에서 웹 사이트 데이터 가져 오기

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

정확한 문제는 안드로이드 앱에서 내 웹 사이트의 데이터를보고 싶지만 표시 할 정보가없는 빈 화면 만 표시됩니다. –

+0

감사합니다. 이 코드를 정확히 어디에 써야합니까? 그것은 큰 도움이 될 것입니다. –

답변

0

간단한 웹 사이트보기

Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://www.hashmedia.in")); 
startActivity(viewIntent); 

이 간단한 코드를 사용하여 Android 앱에서 내 웹 사이트를 볼 수 있습니다.

특정 데이터를 얻으려면 JSON을 사용하여 웹 서비스를 사용하십시오.

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

는 매니페스트 파일에

< 
uses-permission android:name="android.permission.INTERNET" /> 
+0

브라우저 사용자로 연결되는 것 아닙니까? – Darpan

+0

예.하지만이 2 줄의 코드는 초보자에게 도움이됩니다. – stacktry

0

빈 화면을 인터넷 권한을 추가 하시겠습니까? 매니페스트 파일에 인터넷 권한을 정의 했습니까?

<uses-permission android:name="android.permission.INTERNET" /> 
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> 
+0

희망이 도움이 될 수 있습니다! –

관련 문제