2016-06-25 111 views
-1

我是Android開發新手,我需要一些幫助HttpURLConnection如何從我的Android應用發送http GET請求並檢查響應

我想要做的是發送http獲取請求(通過單擊按鈕),然後檢查響應(檢查響應我添加TextView到我的主要活動)。

我的代碼:

public class MainActivity extends AppCompatActivity { 

private ProgressDialog progress; 

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

public void sendGetRequest(View View) { 
    new GetClass(this).execute(); 
} 

private class GetClass extends AsyncTask<String, Void, Void> { 

    private final Context context; 

    public GetClass(Context c){ 
     this.context = c; 
    } 

    protected void onPreExecute(){ 
     progress= new ProgressDialog(this.context); 
     progress.setMessage("Loading"); 
     progress.show(); 
    } 

    @Override 
    protected Void doInBackground(String... params) { 

     String dataUrl = "http://myurl.com"; 
     String dataUrlParameters = "email="+"[email protected]"+"&name="+"priyabrat"; 
     URL url; 
     HttpURLConnection connection = null; 

     try { 
      final TextView outputView = (TextView) findViewById(R.id.showOutput); 
      url = new URL(dataUrl); 

      connection = (HttpURLConnection) url.openConnection(); 
      String urlParameters = "fizz=buzz"; 
      connection.setRequestMethod("GET"); 
      connection.setRequestProperty("USER-AGENT", "Mozilla/5.0"); 
      connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5"); 

      int responseCode = connection.getResponseCode(); 

      final StringBuilder output = new StringBuilder("Request URL " + url); 
      output.append(System.getProperty("line.separator") + "Response Code " + responseCode); 
      output.append(System.getProperty("line.separator") + "Type " + "GET"); 
      BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
      String line = ""; 
      StringBuilder responseOutput = new StringBuilder(); 
      System.out.println("output===============" + br); 
      while((line = br.readLine()) != null) { 
       responseOutput.append(line); 
      } 
      br.close(); 

      output.append(System.getProperty("line.separator") + "Response " + System.getProperty("line.separator") + System.getProperty("line.separator") + responseOutput.toString()); 

      MainActivity.this.runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        outputView.setText(output); 
        progress.dismiss(); 

       } 
      }); 

     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return null; 
    } 
} 

}

問題是,當我運行我的應用程序,然後單擊按鈕「發送GET請求」,應用程序與消息停止「應用程序已停止」

+0

首先添加的到變量dataUrl – has19

+0

末尾的url,並刪除findviewbyid,因爲您只能訪問主線程 – has19

+0

中的視圖並重寫在主線程上運行的onPostExecute方法,您可以在其中修改視圖 – has19

回答

0

看一看一些網絡圖書館的針對Android,他們將處理異步的東西給你:

如果你想爲自己做的,看看從異步任務( https://developer.android.com/reference/android/os/AsyncTask.html)的數獨和使用功能:

onPostExecute(String output) 

後回UI線程調用上地面計算完成。後臺計算的結果作爲參數傳遞給此步驟。

,而不是和:

MainActivity.this.runOnUiThread(new Runnable() { 

    @Override 
    public void run() { 
     outputView.setText(output); 
     progress.dismiss(); 
    } 
}); 

使這個: '?'

return output; 
+0

歡迎您! – Botz

相關問題