2017-10-15 4 views
-1
나는 1 버튼을 가지고

1 글고, 6 확인란과 (다른 활동) 1 텍스트 뷰글고에서 텍스트를 확인하고 체크 박스 상태는 HTTP POST와 URL에이를 보내

확인하면 는 첫째 온 클릭 버튼, 글고에서 텍스트를 가져 변수에 저장하고 확인란가 쳤다되었는지 확인 여부, 재향 군인 생성 : 나는이 원하는 리바일setText to TRUE else FALSE.

둘째 HTTP POST 방법을 통해 URL에 확인란글고의 7 개 변수를 보낼 수 있습니다. 그 후 서버 응답을 받고 그것을 보여주십시오 다른 활동으로 TextView

이것은 내 JAVA 파일이지만 작동하지 않습니다. 누구든지이 코드를 수정할 수 있습니까? 46 : 19130-19130 29.451/com.royalrandhawa.xemp E/AndroidRuntime : FATAL EXCEPTION : 메인 공정 :

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.TextView; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.io.UnsupportedEncodingException; 
import java.net.URL; 
import java.net.URLConnection; 
import java.net.URLEncoder; 

import static com.royalrandhawa.untitled.R.id.mainSearch; 

/** 
* Created by ROYAL on 10/11/2017. 
*/ 

public class SurfAnony extends Activity { 

    String one, two, three, four, five, six; 
    CheckBox First, Second, Third, Fourth, Fifth, Sixth; 
    TextView content; 
    Button search = (Button) findViewById(R.id.surfNowBtn); 
    EditText query = (EditText) findViewById(mainSearch); 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.surf_anony); 

     First = (CheckBox) findViewById(R.id.eurlchb); 
     Second = (CheckBox) findViewById(R.id.epagechb); 
     Third = (CheckBox) findViewById(R.id.alckchb); 
     Fourth = (CheckBox) findViewById(R.id.ftcchb); 
     Fifth = (CheckBox) findViewById(R.id.djschb); 
     Sixth = (CheckBox) findViewById(R.id.dsbchb); 

     //First CheckBox 
     First.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (First.isChecked()) { 
        one = "true"; 
       } else { 
        one = "false"; 
       } 
      } 
     }); 

     //Second CheckBox 
     Second.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (Second.isChecked()) { 
        two = "true"; 
       } else { 
        two = "false"; 
       } 
      } 
     }); 

     //Third CheckBox 

     Third.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (Third.isChecked()) { 
        three = "true"; 
       } else { 
        three = "false"; 
       } 
      } 
     }); 

     //Fourth CheckBox 

     Fourth.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (Fourth.isChecked()) { 
        four = "true"; 
       } else { 
        four = "false"; 
       } 
      } 
     }); 

     //Fifth CheckBox 

     Fifth.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (Fifth.isChecked()) { 
        five = "true"; 
       } else { 
        five = "false"; 
       } 
      } 
     }); 

     //Sixth CheckBox 

     Sixth.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (Sixth.isChecked()) { 
        six = "true"; 
       } else { 
        six = "false"; 
       } 
      } 
     }); 


     search.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       try { 
        GetText(); 
       } catch (Exception ex) { 
        content.setText(" url exeption! "); 
       } 

      } 
     }); 
} 

    public void GetText() throws UnsupportedEncodingException { 
     // Get user defined values 
     String q = query.getText().toString(); 



     // Create data variable for sent values to server 

     String data = URLEncoder.encode("q", "UTF-8") 
       + "=" + URLEncoder.encode(q, "UTF-8"); 

     data += "&" + URLEncoder.encode("eurl", "UTF-8") + "=" 
       + URLEncoder.encode(one, "UTF-8"); 

     data += "&" + URLEncoder.encode("epage", "UTF-8") 
       + "=" + URLEncoder.encode(two, "UTF-8"); 

     data += "&" + URLEncoder.encode("alck", "UTF-8") 
       + "=" + URLEncoder.encode(three, "UTF-8"); 

     data += "&" + URLEncoder.encode("ftc", "UTF-8") 
       + "=" + URLEncoder.encode(four, "UTF-8"); 

     data += "&" + URLEncoder.encode("djs", "UTF-8") 
       + "=" + URLEncoder.encode(five, "UTF-8"); 

     data += "&" + URLEncoder.encode("dsb", "UTF-8") 
       + "=" + URLEncoder.encode(six, "UTF-8"); 

     String text = ""; 
     BufferedReader reader = null; 

     // Send data 
     try { 

      // Defined URL where to send data 
      URL url = new URL("my url here!"); 

      // Send POST data request 

      URLConnection conn = url.openConnection(); 
      conn.setDoOutput(true); 
      OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
      wr.write(data); 
      wr.flush(); 

      // Get the server response 

      reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 

      // Read Server Response 
      while ((line = reader.readLine()) != null) { 
       // Append server response in string 
       sb.append(line + "\n"); 
      } 


      text = sb.toString(); 
     } catch (Exception ex) { 

     } finally { 
      try { 

       reader.close(); 
      } catch (Exception ex) { 
      } 
     } 

     // Show response on activity 
     content.setText(text); 

    } 

} 

는 로그 캣

10-15 16 com.royalrandhawa.xemp , PID : 19130 java.lang.RuntimeException가 : java.lang.NullPointerException이 을 android.ap에서 : 활동 ComponentInfo {com.royalrandhawa.xemp/com.royalrandhawa.xemp.SurfAnony}을 (를) 인스턴스화 할 수 없습니다 안드로이드에서 android.app.ActivityThread.access $ 800 (ActivityThread.java:151)에서 p.ActivityThread.performLaunchActivity android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2390)에서 (ActivityThread.java:2264) .app.ActivityThread $ H.handleMessage (ActivityThread.java:1321) android.os.Handler.dispatchMessage (Handler.java:110)의 at android.os.Looper.loop (Looper.java:193) at android .app.ActivityThread.main (ActivityThread.java:5299) at java.lang.reflect.Method.invokeNative (네이티브 메소드) at java.lang.reflect.Method.invo ke (Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:825) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:641)) dalvik.system.NativeStart.main (기본 방법) 에서 에 의해 발생 :. com.royalrandhawa.xemp.SurfAnony에서 android.app.Activity.findViewById (Activity.java:1899) 에서 java.lang.NullPointerException이 (SurfAnony.java30) at java.lang.Class.newInstanceImpl (네이티브 메소드) at java.lang.Class.newInstance (Class.java:1215) android.app.Instrumentation.newActivit y (계측.자바 : 1061) android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2390) android.app.ActivityThread.access $ 800 에서 android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2255) (ActivityThread에서 된 .java : android.os.Looper.loop에서 android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1321) android.os.Handler.dispatchMessage (Handler.java:110에서 ) 151) (Looper.java:193) android.app.ActivityThread.main (ActivityThread.java:5299) at java.lang.reflect com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:825) 에서 java.lang.reflect.Method.invoke에서 .Method.invokeNative (기본 방법) (Method.java:515) dalvik.system.NativeStart.main에서 com.android.internal.os.ZygoteInit.main (ZygoteInit.java:641) (기본 방법)에서

+0

Put Log(); 각 조건 (if, else)에서 작동하고 문제가있는 LogCat을 살펴보십시오. – Bobo159951

+0

고용주 –

답변

0

당신이에 gettext에 오류가있다();

이 시도 :

public class senddata extends AsyncTask<String,String,String>{ 

    BufferedReader br=null; 
    URL url; 
    String txt=""; 
    HttpURLConnection conn; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

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

      try {    //url of your php file 
       url = new URL("http://192.168.43.226/check.php"); 
      } catch (MalformedURLException m1) { 

       txt = "exception"; 
      } 
      try { 
       //establishing connection 
       conn = (HttpURLConnection) url.openConnection(); 

       conn.setReadTimeout(10000); 
       //connection timeout 
       conn.setConnectTimeout(15000); 
       conn.setDoInput(true); 
       conn.setDoOutput(true); 
       conn.setRequestMethod("POST"); 
       // appending the query parameteres with key 
       Uri.Builder builder = new Uri.Builder() 
         .appendQueryParameter("fname", params[0]) 
         .appendQueryParameter("lname",params[1]) 
         .appendQueryParameter("email",params[2]) 
         .appendQueryParameter("pass",params[3]) 
         .appendQueryParameter("phone",params[4]) 
         .appendQueryParameter("country",params[5]); 
       String query = builder.build().getEncodedQuery(); 
       //starting output stream to upload datat to server 
       OutputStream os = conn.getOutputStream(); 
       BufferedWriter wr = new BufferedWriter(
         new OutputStreamWriter(os, "UTF-8")); 
       //writing the data 
       wr.write(query); 
       wr.flush(); 
       wr.close(); 

       conn.connect(); 


     }catch (IOException e1){ 
      txt = "input"; 
     }try { 

      int response_code = conn.getResponseCode(); 

      // Check if successful connection is established 
      if (response_code == HttpURLConnection.HTTP_OK) { 

       // Read data sent from server 
       InputStream input = conn.getInputStream(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(input)); 
       StringBuilder result = new StringBuilder(); 
       String line; 

       while ((line = reader.readLine()) != null) { 
        result.append(line); 
       } 

       // Pass data to onPostExecute method 
       return (result.toString()); 

      } else { 

       return ("unsuccessful"); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
      txt= "exceptio"; 
     } 

     finally { 
      conn.disconnect(); 
     } 
     return txt; 
      } 







    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 

     Toast.makeText(MainActivity.this,s,Toast.LENGTH_LONG).show(); 
    } 
} 
} 

당신이 사용하여 실행할 수 있습니다 새로운 같이 sendData()을 ("당신이 문자열에 보내려면 지금 무엇을") 실행;.

+0

누군가가 getText() 대신이 정보를 사용하여 서버에 데이터를 보냅니다. –

관련 문제