2017-10-15 5 views
-1
私は 1ボタンを、持っている

1のEditText6チェックボックスと(別の活動中)1のTextViewのEditTextからテキストを取得し、チェックボックスの状態は、HTTP POSTとURLにこれらを送信

まずのonClickボタン、のEditTextからテキストを取得し、それを変数に保存してチェックするとチェックボックスは、チェックさされているかどうかをチェックVA を生成します。私は、これらがしたいですリバーレおよびセットテキストからその他

第二HTTP POST方法を経由してURLにチェックボックスのEditTextの7つの変数を送信します。その後、サーバーの応答を取得して表示しますTextView別のアクティビティ

これは私のJAVAファイルですが、動作しません。誰でもこれらのコードを修正できますか?

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); 

    } 

} 

これはlogCat

10-15 16:46:29.451 19130から19130/com.royalrandhawa.xemp E/AndroidRuntime:致命的な例外:メイン プロセス:com.royalrandhawa.xemp 、PID:19130 java.lang.RuntimeException:できないアクティビティをインスタンス化 ComponentInfo {com.royalrandhawa.xemp/com.royalrandhawa.xemp.SurfAnony} java.lang.NullPointerExceptionが でandroid.ap アンドロイドで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) でandroid .app.ActivityThread.main(ActivityThread.java:5299) at java.lang.reflect.Method.invokeNative(ネイティブメソッド) at java.lang.reflect.Method.invo ke(Method.java:515) ( )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(計装。Javaの:1061) でandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255) android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) でandroid.app.ActivityThread.access $ 800から(ActivityThread .javaファイル:android.os.Looper.loopでandroid.os.Handler.dispatchMessage(Handler.java:110) で151) でandroid.app.ActivityThread $ H.handleMessage(ActivityThread.java:1321) ( 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

Hire誰かがそのために –

答えて

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()の代わりに使って、サーバにデータを送る –

関連する問題