2016-06-25 11 views
-1

私はAndroid開発の新機能で、HttpURLConnectionに関するヘルプが必要です。Androidアプリからhttp GETリクエストを送信してレスポンスを確認する方法

私がしたいのは、(ボタンをクリックして)http getリクエストを送信し、レスポンスをチェックすることです(メインアクティビティに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に移動し、メインスレッド – has19

+0

からのビューにしかアクセスできず、ビューを変更できるメインスレッドで実行されるonPostExecuteメソッドをオーバーライドできるので、findviewbyidを削除してください – has19

答えて

0

AndroidのためのいくつかのネットワークLibrarysを見てください、彼らはあなたのための非同期のものを処理します:

あなた自身のためにそれを行うだろう場合は、顔をしています非同期タスク( https://developer.android.com/reference/android/os/AsyncTask.html)からDOKUでと機能を使用:バック後のUIスレッドで呼び出さ

onPostExecute(String output) 

を地上計算が終了する。バックグラウンド計算の結果は、このステップにパラメータとして渡されます。

、代わりの:

MainActivity.this.runOnUiThread(new Runnable() { 

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

この作る:すべての

return output; 
+0

ようこそ! – Botz

関連する問題