2013-08-07 58 views
2

我瞭解Java,但不幸選擇了Basic4Android for Android Development。工作了一年後,我意識到我應該採用本地解決方案。所以我的問題可能很愚蠢,但我需要你的建議來解決它。Android HTTP GET不起作用

我的目標是從GET請求中檢索數據。我已經在互聯網上嘗試了大量的android http客戶端教程,但每個教程都失敗了。我只是想在這裏分享一個,這樣你可以幫我解決。當我點擊按鈕時,logcat窗口中沒有大量錯誤消息就沒有任何事情發生。

主類是這裏的快速回顧:

public class MainActivity extends Activity implements OnClickListener { 

    private Button Test; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Test = (Button) findViewById(R.id.Test); 
     Test.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     if (v.getId() == R.id.Test){ 
      try { 
       HttpClient client = new DefaultHttpClient(); 
       String getURL = "http://www.google.com"; 
       HttpGet get = new HttpGet(getURL); 
       HttpResponse responseGet = client.execute(get); 
       HttpEntity resEntityGet = responseGet.getEntity(); 
       if (resEntityGet != null) { 
        // do something with the response 
        String response = EntityUtils.toString(resEntityGet); 
        Log.i("GET RESPONSE", response); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      } 
     } 
    } 

而且整個項目是在這裏:https://dl.dropboxusercontent.com/u/15625012/TestHttp.zip

我會很感激任何形式的幫助/建議。

+0

哪個客戶端呢?謝謝。 – Tom

回答

1

您目前正在主UI線程(按鈕點擊功能)中執行網絡訪問。 Android不允許在應用的主線程UI線程中進行長時間操作,例如網絡訪問。您需要在單獨的線程中異步執行此操作。您可以爲此使用內置的異步類。

這裏是一個示例代碼我寫

public class Sample extends Activity 
{ 
private ProgressDialog progress_dialog; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_sample); 

    progress_dialog = new ProgressDialog(this); 
} 

public void MyButtonClick(View view) 
{ 
     EditText usernameEditText = (EditText)findViewById(R.id.sample_username); 
     String username = usernameEditText.getText(); 

     String URL = "http://SOME_WEBSITE?Username=" + username; 

     progress_dialog.setMessage("Loading. Please wait..."); 
    progress_dialog.setCancelable(false); 
    progress_dialog.show(); 

    new SampleAsynThread().execute(URL); 
} 

private class SampleAsynThreadextends AsyncTask <String, Void, String> 
{ 
     protected String doInBackground(String... urls) 
     { 
      // make your request here 
      return "Response"; 
     } 

     protected void onPostExecute(String result) 
     { 
      // show response on ui 
      progress_dialog.dismiss(); 
     } 
    } 

protected void onDestroy() 
{ 
    progress_dialog.dismiss(); 
    super.onDestroy(); 
} 

@Override 
protected void onPause() 
{ 
    progress_dialog.dismiss(); 
    super.onPause(); 
} 

} 
1

首先 - 總是在logcat中發佈錯誤,我們幾乎總是需要他們來解決這個問題。

但是這裏很簡單 - 你不能在主線程上執行網絡IO。您需要在AsyncTask或Thread上運行它。

1
try { 
     URL url = new URL(urlstr); 
     HttpsURLConnection connection = 
        (HttpsURLConnection)url.openConnection(); 
     connection.setReadTimeout(6000); 
     connection.setRequestMethod("GET"); 
     connection.setRequestProperty("User_agent", "android"); 
     OutputStream os = connection.getOutputStream(); 
     //os.write(buffer); 

     InputStream is = connection.getInputStream(); 

    } catch (MalformedURLException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }catch (Exception e) { 
     // TODO: handle exception 
    }