2016-08-17 2 views
1

문제가 있습니다.android 앱 url 연결 요청 요청 작동하지 않음

다음 코드가 작동하지 않는 이유를 알 수 없습니다.

테이블에 행을 삽입해야하는 일부 PHP 조각에 GET 요청을 보내는 간단한 앱입니다.

왜 작동하지 않는지 나는 이해할 수 없다.

도와주세요.

자바 :

package com.example.michael.biyum; 

import android.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

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

public class MainActivity extends AppCompatActivity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
    final AlertDialog alertDialog = alertDialogBuilder.create(); 
    Button startBtn = (Button) findViewById(R.id.btn); 
    final TextView t = (TextView) findViewById(R.id.hello); 
    startBtn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      URL url; 
      HttpURLConnection urlConnection = null; 
      try { 
       url = new URL("http://www.chatty.co.il/biyum.php?q=mkyong"); 
       urlConnection = (HttpURLConnection) url.openConnection(); 
       InputStream in = urlConnection.getInputStream(); 
       InputStreamReader isw = new InputStreamReader(in); 
       int data = isw.read(); 
       while (data != -1) { 
        char current = (char) data; 
        data = isw.read(); 
      Toast.makeText(getApplicationContext(),"GET:"+current,Toast.LENGTH_LONG).show(); 
        System.out.print(current); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } finally { 
       if (urlConnection != null) { 
        urlConnection.disconnect(); 
       } 
      } 
     } 


    }); 
    } 
} 

PHP :

<?php 
require('connect.inc.php'); 
$update_offline="INSERT INTO offline_waiters (user_partner,mail,chat_room) VALUES(0,'gavno','gavno')"; 
$query_run= mysqli_query($conn,$update_offline); 

echo "new"; 



?> 

그것은 내 테이블에 새 행을 입력해야합니다 간단한 테스트입니다.

브라우저를 통해 요청을 수동으로하면 제대로 작동합니다.

다시 한번 문제를 이해하도록 도와주세요.

답변

1

메인 스레드에서 네트워크 작업을 수행해야합니다. 토스트에

startBtn.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      new MyAsyncTask().execute(); 
    }); 

결과 :

enter image description here

class MyAsyncTask extends AsyncTask<Void, Void, String> { 

    @Override 
    protected String doInBackground(Void... params) { 
     StringBuilder result = new StringBuilder(); 
     URL url; 
     HttpURLConnection urlConnection = null; 
     try { 
      url = new URL("http://www.chatty.co.il/biyum.php?q=mkyong"); 
      urlConnection = (HttpURLConnection) url.openConnection(); 
      InputStream in = urlConnection.getInputStream(); 
      InputStreamReader isw = new InputStreamReader(in); 
      int data = isw.read(); 
      while (data != -1) { 
       char current = (char) data; 
       data = isw.read(); 
       result.append(current); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 
      } 
     } 

     return result.toString(); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     Toast.makeText(getApplicationContext(),"GET:"+result,Toast.LENGTH_LONG).show(); 
    } 
} 

는 그냥 클릭 리스너에서 AsyncTask를 실행 :

난 그냥 AsyncTask를 사용하여이 작업을했고 테스트