2016-07-19 5 views
0

"http://192.168.1.1/key_on"과 같은 URL을 HTML 양식으로 보내고 올바르게 변경 될 수 있으면 응답으로 숫자 1 또는 0을 수신하고 싶습니다. 그런 다음 내 앱에서 무언가를하고 싶습니다. AsyncTask에 의한 요청을 보내면 제대로 작동합니다! 그러나 나는 응답을 얻는 방법을 모른다.http에서 응답을받습니다. android

Button btn= (Button)convertView.findViewById(R.id.two); 
btn.setWidth(500); 
new RequestTask().execute("http://192.168.1.1/key_on"); 
btn.setBackgroundColor(Color.parseColor("#FF11AC06")); 
btn.setText(childText); 
btn.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
    int color = Color.TRANSPARENT; 
    Drawable background = view.getBackground(); 
    if (background instanceof ColorDrawable) 
     color = ((ColorDrawable) background).getColor(); 
    if (color == Color.parseColor("#FF11AC06")) { 
     new RequestTask().execute("http://192.168.1.1/key_off"); 
     view.setBackgroundColor(Color.parseColor("#FF41403F")); 
    } else { 
     view.setBackgroundColor(Color.parseColor("#FF11AC06")); 
     new RequestTask().execute("http://192.168.1.1/key_on"); 
    } 
    } 
}); 
return convertView; 

그리고 RequestTask.java에서 :

class RequestTask extends AsyncTask<String, String, String> { 

    @Override 
    protected String doInBackground(String... uri) { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpResponse response; 
     String responseString = null; 
     try { 
      response = httpclient.execute(new HttpGet(uri[0])); 
      StatusLine statusLine = response.getStatusLine(); 
      if(statusLine.getStatusCode() == HttpStatus.SC_OK){ 
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
       response.getEntity().writeTo(out); 
       responseString = out.toString(); 
       out.close(); 
      } else{ 
       //Closes the connection. 
       response.getEntity().getContent().close(); 
       throw new IOException(statusLine.getReasonPhrase()); 
      } 
     } catch (ClientProtocolException e) { 
      //TODO Handle problems.. 
     } catch (IOException e) { 
      //TODO Handle problems.. 
     } 
     return responseString; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     //Do anything with response.. 
    } 
} 
+0

'onPostExecute'가 무엇입니까? – njzk2

+0

'doInBackground()'메소드에서 문자열 값을 반환합니다. 'onPostExecute()'메소드에서 응답 문자열을 얻을 것이다. 그래서'result' 문자열 변수를 출력으로 사용하십시오. – Lawrance

답변

0

귀하의 요청 URL (http://192.168.1.1/key_off)을 확인하시기 바랍니다 귀하의 URL을 발사 code.please 404 오류가 여기에

은 활동 내 일부 코드 편물.