2011-09-22 3 views
2

웹 페이지에서 텍스트를 수집하여 문자열에 넣은 다음 장치 화면에 표시하고 싶습니다.문자열로 Android 웹 요청

이 내 WebRequest 클래스 활동 : 내 장치에서 이클립스에 오류,하지만 응용 프로그램이 충돌이없는

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView android:layout_height="wrap_content" 
    android:id="@+id/textView1" 
    android:text="" 
    android:layout_width="wrap_content"></TextView> 
</LinearLayout> 

package com.work.webrequest; 

import java.io.IOException; 

import org.apache.http.HttpResponse; 
import org.apache.http.HttpStatus; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class WebRequest extends Activity { 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     TextView txt = (TextView) findViewById(R.id.textView1); 
     txt.setText(getPage()); 
    } 

    private String getPage() { 
     String str = "***"; 

     try 
     { 
      HttpClient hc = new DefaultHttpClient(); 
      HttpPost post = new HttpPost("http://zapmenow.co.uk/zapme/?getDetails=true&secret=zjXvwX5frK1po0adXyKJsbbyUe2ZY2PkW9M8r7sb1soIDppIWdTlgt1xmL5VM6g&UDID=401ceca29af68e4569a25e8c16a6987bb8cf1f5a&id=41"); 

      HttpResponse rp = hc.execute(post); 

      if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 
      { 
       str = EntityUtils.toString(rp.getEntity()); 
      } 
     }catch(IOException e){ 
      e.printStackTrace(); 
     } 

     return str; 
    } 


} 

main.xml에. 제발, 최대한 빨리 도움이 필요합니다. PS : 인터넷 권한은 문제가되지 않습니다 그래서 나는, 라인 매니페스트

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

을 추가했습니다. 당신은 여기에서 로그 캣 출력을 게시해야

String response = getPage("http://example.com"); 
+0

:

private String getPage(String url) { HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); con.connect(); if (con.getResponseCode() == HttpURLConnection.HTTP_OK) { return inputStreamToString(con.getInputStream()); } else { return null; } } private String inputStreamToString(InputStream in) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in)); StringBuilder stringBuilder = new StringBuilder(); String line = null; while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line + "\n"); } bufferedReader.close(); return stringBuilder.toString(); } 

나중에에 의해 사용할 수 있습니다 : – Knickedi

답변

6

은 나를 위해 일한 코드입니다. 앱이 충돌하기 때문에 스택 추적이 있어야합니다.
+0

** 힌트 ** : 나는이 코드를 내 옆에 바로 붙 였으므로 나는 당신에게 그것을 주었다. 질문을 게시하기 전에 항상 logcat 출력을 확인해야합니다. 당신은 * 컴파일 오류가 없다고 논쟁 할 수는 없지만 장치에서 충돌합니다 * – Knickedi