2012-09-10 21 views
1

我是一個編程的android noob,但有一些程序的幫助,我可以學習基礎知識。我想對arduino ethernetshield做一個基本的http獲取請求。基本android http獲得

爲此我找到了一些代碼,但是我無法讓它工作。 我一直用我從幾頁中試過的代碼卡住getResponse部分。

我發現下面的頁面這給了我可讀的代碼: How to work with an image using url in android?

現在我已經創建了以下內容: 按一個按鈕,做一個得到一個網址:


package adhttpget.test; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URI; 
import java.net.URL; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Toast; 

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 AdhttpgetActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 


public void pushbutton1(View view) { 
    Toast.makeText(getBaseContext(), "button press", Toast.LENGTH_SHORT).show(); 
      Log.e("button", "pressed"); 

      URL connectURL = new URL("http://192.168.0.48/?out=13&status=2"); 
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection(); 

      // do some setup 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 
      conn.setUseCaches(false); 
      conn.setRequestMethod("GET"); 

      // connect and flush the request out 
      conn.connect(); 
      conn.getOutputStream().flush(); 

      // now fetch the results 
      String response = getResponse(conn); // <-- THIS IS MY PROBLEM 



} 
private String getResponseOrig(HttpURLConnection conn) 
{ 
    InputStream is = null; 
    try 
    { 
     is = conn.getInputStream(); 
     // scoop up the reply from the server 
     int ch; 
     StringBuffer sb = new StringBuffer(); 
     while((ch = is.read()) != -1) { 
      sb.append((char)ch); 
     } 
     return sb.toString(); 
    } 
    catch(Exception e) 
    { 
     Log.e("http", "biffed it getting HTTPResponse"); 
    } 
    finally 
    { 
     try { 
     if (is != null) 
      is.close(); 
     } catch (Exception e) {} 
    } 

    return ""; 
} 

} 

我在哪裏可以找到信息以瞭解如何正確編寫代碼? 或者你碰巧有某種提示的答案,所以我可以從中學習?

+0

不要說你是一個noob在android :)。它不利於問題和答案,它帶來了SO難題:一種封閉的精英形式的馴服 – quinestor

+0

我想我理解你的意思。從來沒有這樣想過:) – Gaston

回答

1

您必須創建一個BufferedReader通過InputStream的,那麼你可以閱讀串

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

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

那麼我建議你進行連接(或讀/寫文件)從主題UI一個separeted線程(線程使用,AsyncTask,Handler等),因爲這會改善你的應用程序。

http://developer.android.com/intl/es/guide/components/processes-and-threads.html

+0

嘗試,但現在看來我有一些閱讀/學習要做。謝謝你的迴應Xagema! – Gaston

+0

這條信息幫助我完成了工作。謝謝@xagema – Gaston